题目描述
blablabla
样例
blablabla
include [HTML_REMOVED]
using namespace std;
int queens;
const int N = 10;
int board[N][N];
bool isConflict(int r, int c) {
for(int i = r; i >= 0; –i)
if(board[i][c]) return true;
for(int i = r, j = c; i >= 0 && j >= 0; –i, –j)
if(board[i][j]) return true;
for(int i = r, j = c; i >= 0 && j < N; –i, ++j)
if(board[i][j]) return true;
return false;
}
void dfs(int l) {
if(l == queens) {
for(int r = 0; r < queens; r) {
for(int c = 0; c < queens; c) {
char printCh = board[r][c] ? ‘Q’ : ‘.’;
cout << printCh;
}
cout << endl;
}
cout << endl;
return;
}
for(int c = 0; c < queens; ++c) {
if(!isConflict(l, c)) {
board[l][c] = 1;
dfs(l+1);
board[l][c] = 0;
}
}
}
int main() {
cin >> queens;
dfs(0);
return 0;
}