n-queens problem
The n-queen problem refers to placing n queens on an n×n chessboard so that the queens cannot attack each other, that is, any two queens cannot be on the same row, column or slope.
Now given an integer n, please output all chess pieces that meet the conditions.
input format
A line containing the integer n.
output format
Each solution occupies n lines, and each line outputs a string of length n, which is used to represent the complete board state. Among them, . indicates that the state of the square at a certain position is empty, and Q indicates that there is a queen on the square at a certain position. After outputting each scheme, output a blank line.
Note: There can be no extra spaces at the end of the line.
The order of the output schemes is arbitrary, as long as there are no repetitions and no omissions.
data range
1≤n≤9
Input sample:
4
Sample output:
.Q..
...Q
Q...
..Q.
..Q.
Q...
...Q
.Q..
Solution
The code above implements a backtracking algorithm to solve the N-Queens problem, which is to place N queens on an NxN chessboard such that no two queens threaten each other. The algorithm uses a depth-first search to explore all possible configurations of queens on the board.
The program first reads an integer n from standard input, which represents the size of the chessboard and the number of queens to be placed. It then initializes a 2D character array g of size nxn to represent the chessboard, where each cell is initially set to '.'. The program also initializes three boolean arrays col, dg, and udg of size n to keep track of which columns and diagonals are occupied by queens.
The program then calls the dfs function with u=0, which represents the current row being considered. The dfs function first checks if u is equal to n. If so, it means that a valid configuration of queens has been found, and the function outputs the chessboard to the console.
If u is less than n, the function enters a loop that iterates over all columns from 0 to n-1. For each column i, the function checks if the cell (u, i) is not threatened by any other queens. This is done by checking if the column i, diagonal u+i, and anti-diagonal n-u+i-1 are not already occupied by queens. If the cell is not threatened, the function places a queen at (u, i) by setting g[u][i] to 'Q', and updates the col, dg, and udg arrays to mark the corresponding column and diagonals as occupied. The function then recursively calls dfs with u+1 as the input.
After the recursive call to dfs returns, the function backtracks by removing the queen from (u, i) and updating the col, dg, and udg arrays to mark the corresponding column and diagonals as unoccupied. This allows the function to explore other possible configurations that start with a different column.
The program outputs all valid configurations of queens on the chessboard to standard output.
Overall, this code is a simple and elegant implementation.
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int n;
char g[N][N];
bool col[N], dg[N], udg[N];
void dfs(int u) {
if (u == n) {
for (int i = 0; i < n; i ++ ) puts(g[i]);
puts("");
return;
}
for (int i = 0; i < n; i ++ ) {
if (!col[i] && !dg[u + i] && !udg[n - u + i]) {
g[u][i] = 'Q';
col[i] = dg[u + i] = udg[n - u + i] = true;
dfs(u + 1);
col[i] = dg[u + i] = udg[n - u + i] = false;
g[u][i] = '.';
}
}
}
int main() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i ++ ) {
for (int j = 0; j < n; j ++ ) {
g[i][j] = '.';
}
}
dfs(0);
return 0;
}