按行枚举
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
char[][] g = new char[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
g[i][j] = '.';
}
}
boolean[] col = new boolean[n], dg = new boolean[2 * n], udg = new boolean[2 * n];
dfs(g, col, dg, udg, n, 0);
}
private static void dfs(char[][] g, boolean[] col, boolean[] dg, boolean[] udg, int n, int size) {
if (size == n) {
for (int i = 0; i < n; i++) System.out.println(g[i]);
System.out.println();
return;
}
for (int j = 0; j < n; j++) {
//第size列没有 Q ,正对角线没有 Q, 反对角线没有Q
if (!col[j] && !dg[size + j] && !udg[j - size + n]) {
g[size][j] = 'Q';
col[j] = dg[size + j] = udg[j - size + n] = true;
dfs(g, col, dg, udg, n, size + 1);
col[j] = dg[size + j] = udg[j - size + n] = false;
g[size][j] = '.';
}
}
}
}
按格子枚举
import java.util.Scanner;
public class Main {
private static char g[][];
private static int n;
private static boolean[] col, row, dg, udg;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
g = new char[n][n];
col = new boolean[n]; row = new boolean[n];
dg = new boolean[2 * n]; udg = new boolean[2 * n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
g[i][j] = '.';
}
}
dfs(0, 0, 0);
}
private static void dfs(int i, int j, int size) {
if (j == n) {j = 0; i++;}
if (i == n ) {
if (size == n) {
for (int k = 0; k < n; k++) System.out.println(g[k]);
System.out.println();
}
return;
}
//不放皇后
dfs(i, j + 1, size);
//放皇后
if (!row[i] && !col[j] && !dg[i + j] && !udg[i - j + n]) {
g[i][j] = 'Q';
row[i] = col[j] = dg[i + j] = udg[i - j + n] = true;
dfs(i, j + 1, size + 1);
row[i] = col[j] = dg[i + j] = udg[i - j + n] = false;
g[i][j] = '.';
}
}
}
em