swan999 @ 2024-01-03 11:37:48
import java.io.BufferedInputStream;
import java.util.*;
public class P1219 {
static int N = 20;
static int n;
static int[][] g = new int[N][N];
static boolean[] row = new boolean[100];
static boolean[] col = new boolean[100];
static boolean[] dg = new boolean[100];
static boolean[] udg = new boolean[100];
static int cnt = 0; // 用来记载解的次数
static List<int[]> ans = new ArrayList<>();
static void dfs(int x,int y,int s) {
if (y == n) {
x ++;
y = 0;
}
if (x == n) {
if (s == n) {
int[] mark = new int[n];
int idx = 0;
cnt ++;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(g[i][j] == 1)
mark[idx ++] = j + 1;
}
}
ans.add(mark);
}
return;
}
//不放置皇后
dfs(x,y + 1,s);
// 放置皇后
if(!row[x] && !col[y] && !dg[y + x] && !udg[y - x + n]){
g[x][y] = 1;
row[x] = true;
col[y] = true;
dg[x + y] = true;
udg[y - x + n] = true;
dfs(x,y + 1,s + 1);
g[x][y] = 0;
row[x] = false;
col[y] = false;
dg[x + y] = false;
udg[y - x + n] = false;
}
}
public static void main(String[] args){
Scanner sc = new Scanner(new BufferedInputStream(System.in));
n = sc.nextInt();
dfs(0,0,0);
Collections.sort(ans, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
for(int i = 0;i < o1.length;i ++){
if(o1[i] != o2[i])
return o1[i] - o2[i];
}
return o1[0] - o2[0];
}
});
for(int i = 0;i < 3;i ++){
int[] res = ans.get(i);
for(int j = 0;j < res.length;j ++)
System.out.print(res[j] + " ");
System.out.println();
}
System.out.println(ans.size());
}
}