Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -6,40 +6,40 @@
#include "../utils/common.hpp"
/* Backtracking algorithm: N queens */
/* Backtracking: 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
// Record a solution when a queen has been placed in every row
if (row == n) {
res.push_back(state);
return;
}
// Traverse all columns
// Iterate over all columns
for (int col = 0; col < n; col++) {
// Calculate the main diagonal and anti-diagonal corresponding to this cell
// Calculate the main-diagonal and anti-diagonal indices for this square
int diag1 = row - col + n - 1;
int diag2 = row + col;
// Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell
// Prune: the square must have no queen in its column, main diagonal, or anti-diagonal
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// Attempt: place the queen in this cell
// Try: place a queen on this square
state[row][col] = "Q";
cols[col] = diags1[diag1] = diags2[diag2] = true;
// Place the next row
// Place a queen in the next row
backtrack(row + 1, n, state, res, cols, diags1, diags2);
// Backtrack: restore this cell to an empty cell
// Backtrack: restore this square to empty
state[row][col] = "#";
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* Solve N queens */
/* Solve the n-queens problem */
vector<vector<vector<string>>> nQueens(int n) {
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
// Initialize an n*n board, where 'Q' represents a queen and '#' represents an empty square
vector<vector<string>> state(n, vector<string>(n, "#"));
vector<bool> cols(n, false); // Record whether there is a queen in the column
vector<bool> diags1(2 * n - 1, false); // Record whether there is a queen on the main diagonal
vector<bool> diags2(2 * n - 1, false); // Record whether there is a queen on the anti-diagonal
vector<bool> cols(n, false); // Track whether a column contains a queen
vector<bool> diags1(2 * n - 1, false); // Track whether a main diagonal contains a queen
vector<bool> diags2(2 * n - 1, false); // Track whether an anti-diagonal contains a queen
vector<vector<vector<string>>> res;
backtrack(0, n, state, res, cols, diags1, diags2);
@@ -47,13 +47,13 @@ vector<vector<vector<string>>> nQueens(int n) {
return res;
}
/* Driver Code */
/* Driver code */
int main() {
int n = 4;
vector<vector<vector<string>>> res = nQueens(n);
cout << "Input board size is " << n << endl;
cout << "Total queen placement solutions: " << res.size() << endl;
cout << "Board width and height: " << n << endl;
cout << "Queen placements: " << res.size() << " solutions" << endl;
for (const vector<vector<string>> &state : res) {
cout << "--------------------" << endl;
for (const vector<string> &row : state) {