Skip to content

Commit

Permalink
Merge pull request #2 from sugyan/feature/optimized-solver
Browse files Browse the repository at this point in the history
Improve OptimizedSolver's table
  • Loading branch information
sugyan committed Sep 20, 2023
2 parents 53ec42c + 20fc518 commit b41f2b9
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions pentomino-solver/src/solvers/optimized.rs
Expand Up @@ -107,7 +107,7 @@ impl SolutionState {
pub struct OptimizedSolver {
rows: usize,
cols: usize,
table: [[Vec<Bitboard>; NUM_PIECES]; 64],
table: [Vec<Vec<(usize, Bitboard)>>; 64],
transposed: bool,
xs: Vec<Bitboard>,
}
Expand All @@ -122,7 +122,7 @@ impl OptimizedSolver {
false
};
let shapes = calculate_shapes();
let mut table = array::from_fn(|_| array::from_fn(|_| Vec::new()));
let mut table = array::from_fn(|_| vec![Vec::new(); 1 << NUM_PIECES]);
let mut xs = Vec::new();
for (n, shape) in shapes.iter().enumerate() {
for s in shape {
Expand All @@ -136,7 +136,11 @@ impl OptimizedSolver {
for y in 0..rows - h {
for x in 0..cols - w {
let offset = x + y * cols;
table[s[0].0 + offset][n].push((v << offset).into());
for i in 0..(1 << NUM_PIECES) {
if (i & (1 << n)) != 0 {
table[s[0].0 + offset][i].push((n, (v << offset).into()));
}
}
// X
if n == X_INDEX && x < (cols - 1) / 2 && y < (rows - 1) / 2 {
xs.push((v << offset).into());
Expand All @@ -153,23 +157,19 @@ impl OptimizedSolver {
xs,
}
}
fn backtrack(&self, current: Bitboard, remain: u32, state: &mut SolutionState) {
fn backtrack(&self, current: Bitboard, remain: usize, state: &mut SolutionState) {
if remain == 0 {
return state.add_solution(
Board(self.represent_solution(&state.pieces)),
self.transposed,
);
}
let target = u64::from(current).trailing_ones() as usize;
for (i, candidates) in self.table[target].iter().enumerate() {
if remain & (1 << i) != 0 {
for &b in candidates.iter() {
if (current & b).is_empty() {
state.pieces[i] = b;
self.backtrack(current | b, remain & !(1 << i), state);
state.pieces[i] = Bitboard::default();
}
}
for &(i, b) in &self.table[target][remain] {
if (current & b).is_empty() {
state.pieces[i] = b;
self.backtrack(current | b, remain & !(1 << i), state);
state.pieces[i] = Bitboard::default();
}
}
}
Expand Down

0 comments on commit b41f2b9

Please sign in to comment.