mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 02:10:37 +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:
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Backtracking algorithm: n queens */
|
||||
/* 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
|
||||
@@ -16,30 +16,30 @@ void backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vect
|
||||
}
|
||||
// Traverse all columns
|
||||
for (int col = 0; col < n; col++) {
|
||||
// Calculate the main and minor diagonals corresponding to the cell
|
||||
// Calculate the main diagonal and anti-diagonal corresponding to this 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
|
||||
// Pruning: do not allow queens to exist in the column, main diagonal, and anti-diagonal of this cell
|
||||
if (!cols[col] && !diags1[diag1] && !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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Solve n queens */
|
||||
/* 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
|
||||
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
|
||||
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<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<vector<vector<string>>> res;
|
||||
|
||||
backtrack(0, n, state, res, cols, diags1, diags2);
|
||||
@@ -52,8 +52,8 @@ int main() {
|
||||
int n = 4;
|
||||
vector<vector<vector<string>>> res = nQueens(n);
|
||||
|
||||
cout << "Input the dimensions of the chessboard as " << n << endl;
|
||||
cout << "Total number of queen placement solutions = " << res.size() << endl;
|
||||
cout << "Input board size is " << n << endl;
|
||||
cout << "Total queen placement solutions: " << res.size() << endl;
|
||||
for (const vector<vector<string>> &state : res) {
|
||||
cout << "--------------------" << endl;
|
||||
for (const vector<string> &row : state) {
|
||||
|
||||
Reference in New Issue
Block a user