mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-21 11:06:07 +00:00
build
This commit is contained in:
@@ -101,9 +101,46 @@ Please note, in an $n$-dimensional matrix, the range of $row - col$ is $[-n + 1,
|
||||
=== "C++"
|
||||
|
||||
```cpp title="n_queens.cpp"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* Backtracking algorithm: n queens */
|
||||
void backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,
|
||||
vector<bool> &diags1, vector<bool> &diags2) {
|
||||
// When all rows are placed, record the solution
|
||||
if (row == n) {
|
||||
res.push_back(state);
|
||||
return;
|
||||
}
|
||||
// Traverse all columns
|
||||
for (int col = 0; col < n; col++) {
|
||||
// Calculate the main and minor diagonals corresponding to the cell
|
||||
int diag1 = row - col + n - 1;
|
||||
int diag2 = row + col;
|
||||
// Pruning: do not allow queens on the column, main diagonal, or minor diagonal of the cell
|
||||
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
|
||||
// Attempt: place the queen in the cell
|
||||
state[row][col] = "Q";
|
||||
cols[col] = diags1[diag1] = diags2[diag2] = true;
|
||||
// Place the next row
|
||||
backtrack(row + 1, n, state, res, cols, diags1, diags2);
|
||||
// Retract: restore the cell to an empty spot
|
||||
state[row][col] = "#";
|
||||
cols[col] = diags1[diag1] = diags2[diag2] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{nQueens}
|
||||
/* Solve n queens */
|
||||
vector<vector<vector<string>>> nQueens(int n) {
|
||||
// Initialize an n*n size chessboard, where 'Q' represents the queen and '#' represents an empty spot
|
||||
vector<vector<string>> state(n, vector<string>(n, "#"));
|
||||
vector<bool> cols(n, false); // Record columns with queens
|
||||
vector<bool> diags1(2 * n - 1, false); // Record main diagonals with queens
|
||||
vector<bool> diags2(2 * n - 1, false); // Record minor diagonals with queens
|
||||
vector<vector<vector<string>>> res;
|
||||
|
||||
backtrack(0, n, state, res, cols, diags1, diags2);
|
||||
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
Reference in New Issue
Block a user