题目描述
blablabla
样例
blablabla
import java.util.Scanner;
public class Main {
static int n;
static char[][] g;
static boolean[] col;
static boolean[] dg;
static boolean[] udg;
public static void dfs(int u){
if(u == n){
for(int i=0;i<n;i++){
for(int j = 0;j<n;j++){
System.out.print(g[i][j]);
}
System.out.println();
}
System.out.println();
return;
}
for(int i=0;i< n;i++){
if(!col[i] && !dg[i+u] && !udg[i -u+n-1]) {
g[u][i] = 'Q';
col[i] = true;
dg[u+i] = true;
udg[i-u+n-1] = true;
dfs(u+1);
col[i] = false;
dg[u+i] = false;
udg[i-u+n-1] = false;
g[u][i] = '.';
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
g = new char[n][n];
col = new boolean[n];
dg = new boolean[2*n-1];
udg = new boolean[2*n-1];
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
g[i][j] = '.';
}
}
dfs(0);
}
}