- 第二期第二次测试
1+1=3!八皇后!
- @ 2025-8-2 13:43:32
#include<bits/stdc++.h>
using namespace std;
int n = 8, m = 8, k = 0;
char g[101][101];
int col[9] = {0} , v1[20] = {0}, v2[20] = {0};
typedef pair<int, int> PII;
vector<PII> q;
struct que{
int x;
int y;
}qq[9];
void print() {
vector<PII> q1 = q;
for(int i = 7;i >= 0; i --){
PII t = q1.back();
qq[i].x = t.first;
qq[i].y = t.second;
q1.pop_back();
}
for(int i = 0;i < 8;i ++){
for(int j = 0;j < 8;j ++){
if(i == qq[i].x && j == qq[i].y){
cout << 1 << " ";
}
else {
cout << 0 << " ";
}
}
cout << endl;
}
// vector<PII> q1 = q;
// for(int i = 0;i < 8;i ++){
// PII t = q1.back();
// cout << "(" << t.first << ", " << t.second << ")" << " ";
// q1.pop_back();
// }
// cout << endl;
}
void DFS(int row){
if(row == 8){
k ++;
cout << "No. " << k << endl;
print();
// cout << q.size() << endl;
return ;
}
for(int i = 0;i < 8;i ++) {
if(!col[i] && !v1[i + row] && !v2[row - i + 7]){
col[i] = 1;
v1[i + row] = 1;
v2[row - i + 7] = 1;
q.push_back({row, i});
DFS(row + 1);
q.pop_back();
col[i] = 0;
v1[i + row] = 0;
v2[row - i + 7] = 0;
}
}
}
int main(){
DFS(0);
// cout << k << endl;
return 0;
}
1 条评论
-
Chon Breise (方瑞宁) LV 9 @ 2025-8-2 14:36:0866666
- 1