mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 15:59:42 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -14,35 +14,35 @@ def backtrack(
|
||||
diags1: list[bool],
|
||||
diags2: list[bool],
|
||||
):
|
||||
"""Backtracking algorithm: n queens"""
|
||||
"""Backtracking algorithm: N queens"""
|
||||
# When all rows are placed, record the solution
|
||||
if row == n:
|
||||
res.append([list(row) for row in state])
|
||||
return
|
||||
# Traverse all columns
|
||||
for col in range(n):
|
||||
# Calculate the main and minor diagonals corresponding to the cell
|
||||
# Calculate the main diagonal and anti-diagonal corresponding to this cell
|
||||
diag1 = row - col + n - 1
|
||||
diag2 = row + col
|
||||
# Pruning: do not allow queens on the column, main diagonal, or minor diagonal of the cell
|
||||
# Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell
|
||||
if not cols[col] and not diags1[diag1] and not diags2[diag2]:
|
||||
# Attempt: place the queen in the cell
|
||||
# Attempt: place the queen in this 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
|
||||
# Backtrack: restore this cell to an empty cell
|
||||
state[row][col] = "#"
|
||||
cols[col] = diags1[diag1] = diags2[diag2] = False
|
||||
|
||||
|
||||
def n_queens(n: int) -> list[list[list[str]]]:
|
||||
"""Solve n queens"""
|
||||
# Initialize an n*n size chessboard, where 'Q' represents the queen and '#' represents an empty spot
|
||||
"""Solve N queens"""
|
||||
# Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
|
||||
state = [["#" for _ in range(n)] for _ in range(n)]
|
||||
cols = [False] * n # Record columns with queens
|
||||
diags1 = [False] * (2 * n - 1) # Record main diagonals with queens
|
||||
diags2 = [False] * (2 * n - 1) # Record minor diagonals with queens
|
||||
cols = [False] * n # Record whether there is a queen in the column
|
||||
diags1 = [False] * (2 * n - 1) # Record whether there is a queen on the main diagonal
|
||||
diags2 = [False] * (2 * n - 1) # Record whether there is a queen on the anti-diagonal
|
||||
res = []
|
||||
backtrack(0, n, state, res, cols, diags1, diags2)
|
||||
|
||||
@@ -54,8 +54,8 @@ if __name__ == "__main__":
|
||||
n = 4
|
||||
res = n_queens(n)
|
||||
|
||||
print(f"Input chessboard dimensions as {n}")
|
||||
print(f"The total number of queen placement solutions is {len(res)}")
|
||||
print(f"Input chessboard size is {n}")
|
||||
print(f"There are {len(res)} queen placement solutions")
|
||||
for state in res:
|
||||
print("--------------------")
|
||||
for row in state:
|
||||
|
||||
Reference in New Issue
Block a user