import java.util.*; public class Queens { int total=0; public int nQueens(int n) { int[][] vis=new int[3][n<<1]; search(0,vis,n); return total; } private void search(int cur, int[][] vis,int n) { if(cur==n){ total++; return; } for(int i=0;i<n;i++){ if(vis[0][i]==0&&vis[1][cur+i]==0&&vis[2][cur-i+n]==0){ vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=1; search(cur+1,vis,n); vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=0; } } } }