HDOJ 2553 N皇后问题
回溯,看了lrj的白书后写的;
一提交,TLE。
# include <stdio.h> # define MAXN 15 int n, ans; char vis[3][MAXN*2+1]; void search(int cur); int main() { while (~scanf("%d", &n)) { if (!n) break; ans = 0; memset(vis, 0, sizeof(vis)); search(0); printf("%d\n", ans); } return 0; } void search(int cur) { int i, j; if (cur == n) ++ans; else for ( i = 0; i < n; ++i) { if (!vis[0][i] && !vis[1][cur+i] && !vis[2][cur-i+n]) { vis[0][i] = vis[1][cur+i] = vis[2][cur-i+n] = 1; search(cur+1); vis[0][i] = vis[1][cur+i] = vis[2][cur-i+n] = 0; } } }
这样的问题还是打表。