题目描述
blablabla
样例
blablabla
java 代码
import java.util.*;
class Main {
static int n=0;
static int arr[]=null;
static boolean flag[]=new boolean[100];
void dfs(int col){
if(!check(col)){
return;
}
if (col>=n) {
for (int i = 0; i <n; i++) {
for (int j = 0; j <n; j++) {
if (j==arr[i])
System.out.print("Q");
else
System.out.print(".");
}
System.out.println();
}
System.out.println();
return;
}
for (int i = 0; i < n; i++) {
if (flag[i]) {
flag[i]=false;
arr[col] = i;
dfs(col + 1);
// arr[col] = 0;
flag[i]=true;
}
}
}
private boolean check(int n) {
for (int i = 0; i <n-1 ; i++) {
for (int j = i+1; j <n ; j++) {
if (Math.abs(arr[i]-arr[j])==Math.abs(i-j))
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
n=scanner.nextInt();
arr=new int[n];
Arrays.fill(flag,true);
new Main().dfs(0);
}
}