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

@@ -9,10 +9,10 @@ package chapter_backtracking;
import java.util.*;
public class n_queens {
/* Backtracking algorithm: N queens */
/* Backtracking: n-queens */
public static void backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,
boolean[] cols, boolean[] diags1, boolean[] diags2) {
// When all rows are placed, record the solution
// Record a solution when a queen has been placed in every row
if (row == n) {
List<List<String>> copyState = new ArrayList<>();
for (List<String> sRow : state) {
@@ -21,28 +21,28 @@ public class n_queens {
res.add(copyState);
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.get(row).set(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.get(row).set(col, "#");
cols[col] = diags1[diag1] = diags2[diag2] = false;
}
}
}
/* Solve N queens */
/* Solve the n-queens problem */
public static List<List<List<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
List<List<String>> state = new ArrayList<>();
for (int i = 0; i < n; i++) {
List<String> row = new ArrayList<>();
@@ -51,9 +51,9 @@ public class n_queens {
}
state.add(row);
}
boolean[] cols = new boolean[n]; // Record whether there is a queen in the column
boolean[] diags1 = new boolean[2 * n - 1]; // Record whether there is a queen on the main diagonal
boolean[] diags2 = new boolean[2 * n - 1]; // Record whether there is a queen on the anti-diagonal
boolean[] cols = new boolean[n]; // Track whether a column contains a queen
boolean[] diags1 = new boolean[2 * n - 1]; // Track whether a main diagonal contains a queen
boolean[] diags2 = new boolean[2 * n - 1]; // Track whether an anti-diagonal contains a queen
List<List<List<String>>> res = new ArrayList<>();
backtrack(0, n, state, res, cols, diags1, diags2);
@@ -65,8 +65,8 @@ public class n_queens {
int n = 4;
List<List<List<String>>> res = nQueens(n);
System.out.println("Input board size is " + n);
System.out.println("Total queen placement solutions: " + res.size() + "");
System.out.println("Input board side length: " + n);
System.out.println("There are " + res.size() + " queen placements");
for (List<List<String>> state : res) {
System.out.println("--------------------");
for (List<String> row : state) {