Eight queens problem
https://www.luogu.org/recordnew/show/19470456
- This problem was first thought was explosive search each node possible, using the back were looking ,, but this time complexity is certainly Index explosion
- We traversed once for each layer can
precisely place each per row and a queen, if C [x] represents a column number row x Queen, the problem becomes full permutation generation problem. And n is 0 to arranged a total of only n!, At least smaller than the index.
import java.util.Scanner;
public class Main{
private int[] col = new int[20];
private boolean[][] use = new boolean[3][100];
private int size;
private int[] result = new int[20];
private int ans = 0;
public void get_data(){
Scanner in = new Scanner(System.in);
size= in.nextInt();
}
public void dfs(int i){
if(i==size+1) {
ans++;
if(ans<=3)
this.output();
return;
}
for(int j=1;j<=size;j++){
if(!use[0][j]&&!use[1][i+j]&&!use[2][i-j+size]) {
result[i] = j;
use[0][j] = use[1][i+j] = use[2][i-j+size] = true;
dfs(i+1);
use[0][j] = use[1][i+j] = use[2][i-j+size] = false;
}
}
}
public void solve(){
dfs(1);
System.out.println(ans);
}
public void output(){
for(int i=1;i<=size;i++){
System.out.print(result[i]+" ");
}
System.out.println();
}
public static void main(String arg[]){
Main obj = new Main();
obj.get_data();
obj.solve();
}
}