八皇后旧事重提
作者:
jy9
,
2024-10-23 21:24:31
,
所有人可见
,
阅读 1
#include <iostream>
#include <cstring>
using namespace std;
bool row[15], col[15], udp[100],fudp[100];
int n;
char mp[15][15];
void dfs(int r){
if(r > n){
for(int i = 1; i <= n; i++){
for (int j = 1; j <= n; j ++ ){
cout << mp[i][j];
}
cout << endl;
}
cout << endl;
}
for (int i = 1; i <= n; i ++ ){
if(col[i] || udp[r-i+n] || fudp[r+i])continue;
col[i] = udp[r-i+n] = fudp[r+i] = 1;
mp[r][i] = 'Q';
dfs(r+1);
mp[r][i] = '.';
col[i] = udp[r-i+n] = fudp[r+i] = 0;
}
}
int main()
{
cin >> n;
memset(row, 0, sizeof row);
memset(col, 0, sizeof col);
memset(udp, 0, sizeof udp);
memset(fudp, 0, sizeof fudp);
memset(mp, '.', sizeof mp);
dfs(1);
return 0;
}