【codevs1295】N皇后问题
problem
solution
codes
//c[i]:第i行的皇后放在第几列
#include<iostream>
using namespace std;
int n, c[20], ans;
void dfs(int cur){
if(cur > n)ans++;
else for(int i = 1; i <= n; i++){
int ok = 1;
for(int j = 1; j < cur; j++)
if(c[j]==i || c[j]-j==i-cur || c[j]+j==i+cur)
{ ok = 0; break; }
if(ok){
c[cur] = i;
dfs(cur+1);
}
}
}
int main(){
cin>>n;
dfs(1);
cout<<ans<<"\n";
return 0;
}