Created
February 10, 2019 15:24
-
-
Save JPMoresmau/8763a50adb796214f0df3d76b64b684d to your computer and use it in GitHub Desktop.
Lovecraft: try_all
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
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