mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-04 03:34:21 +00:00
2778a6f9c7
* 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
77 lines
2.3 KiB
Rust
77 lines
2.3 KiB
Rust
/*
|
|
* File: n_queens.rs
|
|
* Created Time: 2023-07-15
|
|
* Author: codingonion (coderonion@gmail.com)
|
|
*/
|
|
|
|
/* Backtracking algorithm: N queens */
|
|
fn backtrack(
|
|
row: usize,
|
|
n: usize,
|
|
state: &mut Vec<Vec<String>>,
|
|
res: &mut Vec<Vec<Vec<String>>>,
|
|
cols: &mut [bool],
|
|
diags1: &mut [bool],
|
|
diags2: &mut [bool],
|
|
) {
|
|
// When all rows are placed, record the solution
|
|
if row == n {
|
|
res.push(state.clone());
|
|
return;
|
|
}
|
|
// Traverse all columns
|
|
for col in 0..n {
|
|
// Calculate the main diagonal and anti-diagonal corresponding to this cell
|
|
let diag1 = row + n - 1 - col;
|
|
let diag2 = row + col;
|
|
// 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 this cell
|
|
state[row][col] = "Q".into();
|
|
(cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);
|
|
// Place the next row
|
|
backtrack(row + 1, n, state, res, cols, diags1, diags2);
|
|
// Backtrack: restore this cell to an empty cell
|
|
state[row][col] = "#".into();
|
|
(cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Solve N queens */
|
|
fn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {
|
|
// Initialize an n*n chessboard, where 'Q' represents a queen and '#' represents an empty cell
|
|
let mut state: Vec<Vec<String>> = vec![vec!["#".to_string(); n]; n];
|
|
let mut cols = vec![false; n]; // Record whether there is a queen in the column
|
|
let mut diags1 = vec![false; 2 * n - 1]; // Record whether there is a queen on the main diagonal
|
|
let mut diags2 = vec![false; 2 * n - 1]; // Record whether there is a queen on the anti-diagonal
|
|
let mut res: Vec<Vec<Vec<String>>> = Vec::new();
|
|
|
|
backtrack(
|
|
0,
|
|
n,
|
|
&mut state,
|
|
&mut res,
|
|
&mut cols,
|
|
&mut diags1,
|
|
&mut diags2,
|
|
);
|
|
|
|
res
|
|
}
|
|
|
|
/* Driver Code */
|
|
pub fn main() {
|
|
let n: usize = 4;
|
|
let res = n_queens(n);
|
|
|
|
println!("Input board size is {n}");
|
|
println!("Total queen placement solutions: {}", res.len());
|
|
for state in res.iter() {
|
|
println!("--------------------");
|
|
for row in state.iter() {
|
|
println!("{:?}", row);
|
|
}
|
|
}
|
|
}
|