解题思路:
注意事项:
参考代码:
看题解没有java 来个java题解
import java.util.Scanner; public class Main09 { public static int[][] arr; public static int[] num; public static int n; public static boolean check(int x,int y) { //左 for (int i = y+1; i <n ; i++) { if(arr[x][i]==2) { return false; }else if(arr[x][i]==0) { break; } } //右 for (int i = y-1; i >=0 ; i--) { if(arr[x][i]==2) { return false; }else if(arr[x][i]==0) { break; } } //下 for (int i = x+1; i <n; i++) { if(arr[i][y]==2) { return false; }else if(arr[i][y]==0) { break; } } //上 for (int i = x-1; i >=0; i--) { if(arr[i][y]==2) { return false; }else if(arr[i][y]==0) { break; } } return true; } public static void dfs(int x,int y,int k) { if(k<=n*n) { num[k]++; } for (int i = x; i <n; i++) { int j; if(i==x) { j=y; }else { j=0; } for (;j<n; j++) { if(arr[i][j]!=0&&arr[i][j]!=2&&check(i,j)==true) { arr[i][j]=2; dfs(i,j,k+1); arr[i][j]=1; } } } } public static void main(String[] args) { Scanner in=new Scanner(System.in); n=in.nextInt(); arr=new int[n][n]; num=new int[n*n+1]; for (int i = 0; i <n; i++) { for (int j = 0; j <n; j++) { arr[i][j]=in.nextInt(); } } dfs(0,0,1); for (int i = 2; i <=n*n; i++) { if(num[i]!=0) { System.out.println(num[i]); } } } }
0.0分
2 人评分