import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static int[] colum;
static int ans = 0;
static int n = 0;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
n = scan.nextInt();
colum = new int[n+1];
dfs(1);
System.out.println(ans);
scan.close();
}
private static void dfs(int row) {
if (row>n) {
ans++;
}
for (int i = 1; i <= n; i++) {
if (check(row, i)) {
colum[row] = i;
dfs(row+1);
}
}
}
private static boolean check(int r,int l) {//检查行和列
for (int i = 1; i < r; i++) {
if (l==colum[i]||(Math.abs(r-i)==Math.abs(l-colum[i]))&&(r-i)<3) {
return false;
}
}
return true;
}
}
受伤的皇后dfs
最新推荐文章于 2024-08-22 17:27:46 发布