0层:神圣打表(∞)
1 #include <cstdio> 2 int main() { 3 int arr[11] = {0,1,0,0,2,10,4,40,92,352,724}; 4 int n; 5 while(~scanf("%d", &n) && n){ 6 printf("%d\n", arr[n]); 7 } 8 }
1层:一维暴力回溯(13)
1 const int maxn = 20; 2 int G[maxn]; //G[c] = r 3 int n, cnt; 4 5 bool ok(int cc) { 6 for(int rr = 0; rr < cc; rr++) { 7 if(G[rr] == G[cc] || abs(rr-cc) == abs(G[rr]-G[cc])) { 8 return false; 9 } 10 } 11 return true; 12 } 13 14 void dfs(int cc) { 15 if(cc == n) { 16 cnt++; 17 return ; 18 } 19 for(int rr = 0; rr < n; rr++) { 20 G[cc] = rr; 21 if(ok(cc)) { 22 dfs(cc + 1); 23 } 24 } 25 }
时间: 2024-10-06 21:01:50