【HDOJ】2553 N皇后问题
超级经典神搜,这题以前做过,知道需要打表。没优化。
#include <stdio.h> #include <string.h> #define MAXNUM 12 /* int queens[MAXNUM][MAXNUM]; int count; int dir[4][2] = {{-1,-1},{-1,1},{1,1},{1,-1}}; void dfs(int c, int n) { int i, j, tmp; int x, y, dx, dy; for (i=1; i<=n; ++i) { tmp = 1; for (j=0; j<4; ++j) { dx = dir[j][0]; dy = dir[j][1]; x = c + dx; y = i + dy; while (x>=1 && x<=n && y>=1 && y<=n) { if (queens[x][y]) { tmp = 0; break; } x += dx; y += dy; } if (tmp == 0) break; } for (j=1; j<=n; ++j) if (queens[j][i]) { tmp = 0; break; } if (tmp) { if (c == n) { count++; return ; } queens[c][i] = 1; dfs(c+1, n); queens[c][i] = 0; } } } int main() { int n; while (scanf("%d", &n)!=EOF && n) { memset(queens, 0, sizeof(queens)); count = 0; dfs(1, n); printf("%d\n", count); } return 0; } */ int kinds[] = {0, 1, 0, 0, 2, 10, 4, 40, 92, 352, 724}; int main() { int n; while (scanf("%d", &n)!=EOF && n) { printf("%d\n", kinds[n]); } return 0; }