N皇后

tech2023-12-28  80

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<cstring> bool h[100], x[100], y[100]; int n, a[100], ans = 0; void dfs(int k) { int i; if (k == n + 1) { ans++; for (i = 1; i < n; i++) { printf("% d", a[i]); } printf("% d\n", a[i]); for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) if (k == a[j])printf("Q"); else printf(". "); printf("\n"); } return; } for (i = 1; i <= n; i++) { if (h[i] && x[i - k + n] && y[i + k]) { a[k] = i; h[i] = x[i - k + n] = y[i + k] = 0; dfs(k + 1); h[i] = x[i - k + n] = y[i + k] = 1; } } } int main() { scanf("%d", &n); memset(h, 1, sizeof(h)); memset(x, 1, sizeof(x)); memset(y, 1, sizeof(y)); dfs(1); printf("%d\n", ans); return 0; }
最新回复(0)