Leetcode 原题.
这里 N 最大会取到 13, TLE 了
代码
#include <iostream>
#include <stdio.h>
using namespace std; bool chess[][];
int n; int cnt;
void dfs(int depth) {
if(depth == n) {
cnt ++;
return;
} for(int i = ; i < n; i ++) { bool qualify = true;
for(int j = depth-; j >= ; j --) {
if(chess[j][i]) {
qualify = false;
break;
}
} if(!qualify)
continue; for(int k = ; depth-k >= && i-k >= ; k++) {
if(chess[depth-k][i-k]) {
qualify = false;
break;
}
} if(!qualify)
continue; for(int k = ; depth-k >= && i+k < n; k ++) {
if(chess[depth-k][i+k]) {
qualify = false;
break;
}
} if(!qualify) {
continue;
} // qualified chess[depth][i] = true;
dfs(depth + );
chess[depth][i] = false;
}
} int main() {
while(scanf("%d", &n) != EOF) {
cnt = ; dfs(); cout << cnt << endl;
}
return ;
}