等我提交的时候,没时间了。。。 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); int n = Integer.parseInt(line); int[][] area = new int[n][n]; for (int i = 0; i < n; i++) { line = scanner.nextLine(); String[] split = line.split(","); if (split.length != n) { throw new IllegalArgumentException("错误输入"); } int j = 0; for (String num : split) { area[i][j++] = Integer.parseInt(num); } } int minimumTimeCost = getMinimumTimeCost(n, area); System.out.println(minimumTimeCost); } private static int getMinimumTimeCost(int n, int[][] area) { int res = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { int t = dfs(area, 0, i, n, 0); // System.out.println("res:" + t); if (t < res && t != -1) res = t; } return res; } static int dfs(int[][] area, int i, int j, int n, int t) { // System.out.println(i + " " + j); if (i > n || j >= n) return -1; if (i == n) return t; if (j == n - 1) return dfs(area, i += 2, j, n, t += area[i - 1][j]); if (area[i + 1][j] <= area[i][j + 1]) return dfs(area, i += 2, j, n, t += area[i - 1][j]); return dfs(area, i, j += 2, n, t += area[i][j - 1]); } }
点赞 评论

相关推荐

不愿透露姓名的神秘牛友
10-12 10:48
已编辑
秋冬春招之苟:邻居家老哥19届双2硕大厂开发offer拿遍了,前几天向他请教秋招,他给我看他当年的简历,0实习实验室项目技术栈跟开发基本不沾边😂,我跟他说这个放在现在中厂简历都过不了
点赞 评论 收藏
分享
牛客网
牛客企业服务