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

@@ -4,51 +4,51 @@
* Author: Justin (xiefahit@gmail.com)
*/
/* Backtracking algorithm: N queens */
/* Backtracking: n-queens */
function backtrack(row, n, state, res, cols, diags1, 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(state.map((row) => row.slice()));
return;
}
// Traverse all columns
// Iterate over all columns
for (let 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
const diag1 = row - col + n - 1;
const 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 */
function nQueens(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
const state = Array.from({ length: n }, () => Array(n).fill('#'));
const cols = Array(n).fill(false); // Record whether there is a queen in the column
const diags1 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the main diagonal
const diags2 = Array(2 * n - 1).fill(false); // Record whether there is a queen on the anti-diagonal
const cols = Array(n).fill(false); // Track whether a column contains a queen
const diags1 = Array(2 * n - 1).fill(false); // Track whether a main diagonal contains a queen
const diags2 = Array(2 * n - 1).fill(false); // Track whether an anti-diagonal contains a queen
const res = [];
backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
// Driver Code
// Driver code
const n = 4;
const res = nQueens(n);
console.log(`Input board size is ${n}`);
console.log(`Total queen placement solutions: ${res.length}`);
console.log(`Input board side length: ${n}`);
console.log(`Number of queen arrangements: ${res.length}`);
res.forEach((state) => {
console.log('--------------------');
state.forEach((row) => console.log(row));