解题思路: 思路其实就是上下左右走,然后记录好路径就行了
注意事项:
参考代码:
private static boolean v[][]; //这个是目标靶子数据 private static int l[][]; //这个运行时候的靶子数据 private static int t[][]; //控制方向 private static int dif[][] = {{0,-1},{0,1},{-1,0},{1,0}}; //记录路径 private static int[][] walk; private static int n; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); v = new boolean[n][n]; l = new int[2][n]; t = new int[2][n]; walk = new int[n][n]; for (int i = 0; i < n; i++) { Arrays.fill(walk[i],-1); } //北边的靶子数 for (int i = 0; i < n; i++) { l[0][i] = scanner.nextInt(); } //西边的靶子数 for (int i = 0; i < n; i++) { l[1][i] = scanner.nextInt(); } dfs(0,0,0); } public static void dfs(int row,int col,int count){ if (row < 0 || col < 0 ||row >= v.length || col >= v[0].length) return; if (v[row][col]) return; for (int k = 0; k < dif.length; k++) { v[row][col] = true; t[0][col] += 1; t[1][row] += 1; walk[row][col] = count++; if (row == n-1 && col == n-1) { boolean op = true; for (int i = 0; i < l.length; i++) { for (int j = 0; j < l[i].length; j++) { if (l[i][j] != t[i][j]) op = false; } } if (op){ int cou = 0; String pr = new String(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (walk[i][j] == cou){ pr += i*n+j+" "; i = 0;j =0 ; cou++; } } } System.out.println(pr.substring(0,pr.length()-1)); return; } } dfs(row+dif[k][0],col+dif[k][1],count); v[row][col] = false; t[0][col] -= 1; t[1][row] -= 1; walk[row][col] = -1; count--; } }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题9.3 (Java代码)浏览:955 |
多输入输出练习1 (C语言代码)浏览:1175 |
数组输出 (C语言代码)--此题的题目描述有问题浏览:1812 |
A+B for Input-Output Practice (III) (C语言代码)浏览:569 |
简单的a+b (C语言代码)浏览:596 |
WU-复数求和 (C++代码)浏览:1995 |
printf基础练习2 (C语言代码)浏览:644 |
【求[X,Y]内被除3余1并且被除5余3的整数的和】 (C语言代码)浏览:672 |
循环入门练习6 (C语言代码)浏览:951 |
sizeof的大作用 (C语言代码)浏览:1022 |