-
-
Save kindlychung/cb3ae61c817f97554a6f405f08722a55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
warning: variable does not need to be mutable, #[warn(unused_mut)] on by default | |
--> src/eight_queens.rs:43:6 | |
| | |
43 | let mut start: usize; | |
| ^^^^^^^^^ | |
Finished debug [unoptimized + debuginfo] target(s) in 0.38 secs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::cmp::{max, min}; | |
const board_size: usize = 8usize; | |
fn is_valid(row: usize, col: usize, pos: &Vec<usize>) -> bool { | |
// any position in the 1st row should always be valid. | |
if row == 0 { | |
return true; | |
} | |
// check all previous rows | |
for r in 0 .. row { | |
// same column -> invalid | |
// on the same diagonal -> invalid | |
if pos[r] == col || (row - r) == max(col, pos[r]) - min(col, pos[r]) { | |
return false; | |
} | |
} | |
true | |
} | |
#[test] | |
fn test_valid() { | |
let mut pos: Vec<usize> = vec![0, 8, 8, 8, 8, 8, 8, 8,]; // initialize the vector with size of board, indices are 0 .. (board_size - 1) | |
assert!(is_valid(0, 0, &pos)); | |
assert!(is_valid(0, 1, &pos)); | |
assert!(is_valid(0, 3, &pos)); | |
assert!(! is_valid(1, 0, &pos)); | |
assert!(! is_valid(2, 0, &pos)); | |
assert!(! is_valid(3, 0, &pos)); | |
assert!(! is_valid(4, 0, &pos)); | |
assert!(! is_valid(5, 0, &pos)); | |
assert!(! is_valid(6, 0, &pos)); | |
assert!(! is_valid(1, 1, &pos)); | |
assert!(! is_valid(2, 2, &pos)); | |
assert!(! is_valid(3, 3, &pos)); | |
assert!(! is_valid(4, 4, &pos)); | |
assert!(! is_valid(5, 5, &pos)); | |
assert!(! is_valid(6, 6, &pos)); | |
} | |
fn find_position(row: usize, pos: &Vec<usize>) -> usize { | |
let mut start: usize; | |
if pos[row] == board_size { | |
start = 0; | |
} else { | |
start = pos[row] + 1; // this is the reaction to backtracking, i.e. move to the next col | |
} | |
for j in start..board_size { | |
if is_valid(row, j, pos) { | |
return j; | |
} | |
} | |
return board_size; | |
} | |
#[test] | |
fn test_find_pos() { | |
let mut pos: Vec<usize> = vec![8, 8, 8, 8, 8, 8, 8, 8,]; | |
assert_eq!(find_position(0, &pos), 0usize); | |
let mut pos: Vec<usize> = vec![0, 8, 8, 8, 8, 8, 8, 8,]; | |
assert_eq!(find_position(1, &pos), 2usize); | |
let mut pos: Vec<usize> = vec![1, 8, 8, 8, 8, 8, 8, 8,]; | |
assert_eq!(find_position(1, &pos), 3usize); | |
let mut pos: Vec<usize> = vec![2, 8, 8, 8, 8, 8, 8, 8,]; | |
assert_eq!(find_position(1, &pos), 0usize); | |
let mut pos: Vec<usize> = vec![0, 2, 8, 8, 8, 8, 8, 8,]; | |
assert_eq!(find_position(2, &pos), 4usize); | |
let mut pos: Vec<usize> = vec![1, 3, 8, 8, 8, 8, 8, 8,]; | |
assert_eq!(find_position(2, &pos), 0usize); | |
} | |
pub fn queens() -> Option<Vec<usize>> { | |
let mut pos = vec![board_size; board_size]; // initialize the vector with size of board, indices are 0 .. (board_size - 1) | |
// find a valid position for k-th row | |
let mut row = 0usize; | |
while row < board_size { | |
let j = find_position(row, &mut pos); | |
pos[row] = j; | |
if j != board_size { | |
row += 1; | |
} else { | |
row -= 1; | |
} | |
} | |
return Some(pos); | |
} | |
#[test] | |
fn test_queens() { | |
assert_eq!(queens(), Some(vec![0usize, 4, 7, 5, 2, 6, 1, 3])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment