import java.io.*;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int n,cnt,sum;
static boolean[][] judge,result;
static int[][] count;
public static void main(String[] args) throws IOException {
n = Integer.parseInt(br.readLine());
judge = new boolean[n+1][n+1];
result = new boolean[n+1][n+1];
count = new int[n+1][n+1];
cnt = 0;
sum = 0;
DFS(1);
System.out.print(sum);
br.close();
}
public static void DFS(int layer){
if(layer == n + 1){
sum++;
cnt++;
if(cnt <= 3) {
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= n ; j++){
if(result[i][j]){
System.out.print(j + " ");
}
}
}
System.out.println();
}
return ;
}
int i,j,k;
for(i = 1 ; i <= n ; i++){
if(!judge[layer][i]){
result[layer][i] = true;
for(j = layer ; j <= n ; j++){
judge[j][i] = true;
count[j][i]++;
}
for(j = i + 1 , k = layer + 1 ; j <= n && k <= n ; j++ , k++){
judge[k][j] = true;
count[k][j]++;
}
for(j = i - 1 , k = layer + 1 ; k <= n && j >= 0 ; k++ , j--){
judge[k][j] = true;
count[k][j]++;
}
DFS(layer + 1);
result[layer][i] = false;
for(j = layer ; j <= n ; j++){
count[j][i]--;
if(count[j][i] == 0){
judge[j][i] = false;
}
}
for(j = i + 1 , k = layer + 1 ; j <= n && k <= n ; j++ , k++){
count[k][j]--;
if(count[k][j] == 0){
judge[k][j] = false;
}
}
for(j = i - 1 , k = layer + 1 ; k <= n && j >= 0 ; k++ , j--){
count[k][j]--;
if(count[k][j] == 0){
judge[k][j] = false;
}
}
}
}
}
}
洛谷 P1219 [USACO1.5] 八皇后 Checker Challenge Java
最新推荐文章于 2024-05-31 10:46:10 发布