Code shared from the Rust Playground
| // Compile with: rustc -C opt-level=3 -C target-cpu=native | |
| const SIDE: usize = 6; | |
| type BoardType = u64; | |
| struct Board { squares: BoardType } | |
| impl Board { | |
| fn new() -> Self { | |
| /*static_*/assert!(std::mem::size_of::<BoardType>() * 8 >= SIDE * SIDE); | |
| Self { squares: 0 } | |
| } | |
| fn toggle_piece(&mut self, i: usize, j: usize) { | |
| self.squares ^= 1 << (i * SIDE + j); | |
| } | |
| fn has_been_visited(&self, i: usize, j: usize) -> bool { | |
| (self.squares & (1 << (i * SIDE + j))) != 0 | |
| } | |
| } | |
| fn robot_paths() -> usize { | |
| fn robot_paths(board: &mut Board, i: usize, j: usize) -> usize { | |
| if i == SIDE - 1 && j == SIDE - 1 { | |
| return 1; | |
| } | |
| board.toggle_piece(i, j); | |
| let mut result = 0; | |
| for &(s0, s1) in [(0, 1), (1, 0), (-1, 0), (0, -1)].iter() { | |
| let i = (i as isize).wrapping_add(s0) as usize; | |
| let j = (j as isize).wrapping_add(s1) as usize; | |
| if i < SIDE && j < SIDE && !board.has_been_visited(i, j) { | |
| result += robot_paths(board, i, j); | |
| } | |
| } | |
| board.toggle_piece(i, j); | |
| result | |
| } | |
| robot_paths(&mut Board::new(), 0, 0) | |
| } | |
| fn main() { | |
| println!("{:?}", robot_paths()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment