1 条题解
-
0
C++ :
#include<iostream> #include<cstdio> using namespace std; const int maxn = 10; // 存储矩阵 int n; int tot = 0; // 统计解的个数 int site[maxn][maxn]; int W[maxn]; int B[maxn]; // 第i行黑皇后的位置 // 放黑皇后 void search_black(int cur) { if(cur == n) { tot++; } else for(int i = 0; i < n; i++) { // 也得确保没在此处放白皇后 if(W[cur] != i && site[cur][i]) { int ok = 1; B[cur] = i; for(int j = 0; j < cur; j++) { if(B[cur] == B[j] || cur-B[cur] == j-B[j] || cur+B[cur] == j+B[j]) { ok = 0; break; } } if(ok) search_black(cur+1); } } } // 放白皇后 void search_white(int cur) { if(cur == n) { search_black(0); } else for(int i = 0; i < n; i++) { if(site[cur][i]) { int ok = 1; W[cur] = i; for(int j = 0; j < cur; j++) { if(W[cur] == W[j] || cur-W[cur] == j-W[j] || cur+W[cur] == j+W[j]) { ok = 0; break; } } if(ok) search_white(cur+1); } } } int main() { //freopen("1.txt", "r", stdin); //freopen("2.txt", "w", stdout); int M; cin >> M; while(M--) { tot = 0; cin >> n; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { cin >> site[i][j]; } } search_white(0); cout << tot << endl; } return 0; }
- 1
信息
- ID
- 2888
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- (无)
- 递交数
- 0
- 已通过
- 0
- 上传者