Skip to content

Instantly share code, notes, and snippets.

@JPMoresmau
Created February 10, 2019 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JPMoresmau/8763a50adb796214f0df3d76b64b684d to your computer and use it in GitHub Desktop.
Save JPMoresmau/8763a50adb796214f0df3d76b64b684d to your computer and use it in GitHub Desktop.
Lovecraft: try_all
fn try_all(grid: &Grid,
seen: &mut HashMap<Grid,Vec<Pos>>,
todo: &mut VecDeque<Grid>
) -> Option<Vec<Pos>> {
let path= match seen.get(grid){
Some(p) => p.clone(),
None => panic!("no path to grid!")
};
for a in 0..4 {
for b in 0..4 {
let g =swap(&grid,(a,b));
let mut path = path.clone();
path.push((a,b));
match seen.get(&g){
Some(v) => if v.len()>path.len() {
seen.insert(g,path.clone());
},
None => {
seen.insert(g,path.clone());
todo.push_back(g);
},
}
if is_solved(g){
return Some(path);
}
}
}
None
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment