Skip to content

Instantly share code, notes, and snippets.

@karlbeecken
Last active February 17, 2023 11:32
Show Gist options
  • Save karlbeecken/014aaa25197b53a8d8d93817e130aa51 to your computer and use it in GitHub Desktop.
Save karlbeecken/014aaa25197b53a8d8d93817e130aa51 to your computer and use it in GitHub Desktop.
fn main() {
const N: i32 = 8;
let mut loesung = [0; N as usize];
let mut anzahl = 0;
fn passt_dame(zeile: i32, spalte: i32, loesung: &mut [i32]) -> bool {
for row in 0..zeile {
// check vertical, horizontal and diagonal
if loesung[row as usize] == spalte {
return false;
}
if loesung[row as usize] == spalte - (zeile - row) {
return false;
}
if loesung[row as usize] == spalte + (zeile - row) {
return false;
}
}
true
}
fn setze_dame(zeile: i32, lsng: &mut [i32], anz: &mut i32) {
for spalte in 0..N {
if passt_dame(zeile, spalte, lsng) {
lsng[zeile as usize] = spalte;
if zeile == N - 1 {
//println!("{:?}", lsng);
print!(".");
*anz += 1;
} else {
setze_dame(zeile + 1, lsng, anz);
}
}
}
}
let start = std::time::Instant::now();
setze_dame(0, &mut loesung, &mut anzahl);
let end = std::time::Instant::now();
println!("\n");
println!("Time: {:?}", end - start);
println!("Anzahl der Loesungen: {anzahl}");
println!("Loesungen/s: {}", anzahl as f64 / (end - start).as_secs_f64());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment