Created
September 25, 2014 18:40
-
-
Save ibebrett/f242dcef85b836789f64 to your computer and use it in GitHub Desktop.
Shortest Path
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
pathRecurse :: Board -> [Pos] -> Pos -> Pos -> Maybe [Pos] | |
pathRecurse b visited pa pb | |
| pa == pb = Just (visited++[pb]) | |
| null neighbors = Nothing | |
| null subpaths = Nothing | |
| otherwise = Just ( minimumBy (comparePath) subpaths ) | |
where subpaths = catMaybes ( map (\n -> pathRecurse b (visited++[pa]) n pb) neighbors) | |
neighbors = filter (\x -> (walkable b x) && not (x `elem` visited)) (getNeighbors b pa) | |
comparePath x y = compare (length x) (length y) | |
path :: Board -> Pos -> Pos -> Maybe [Pos] | |
path b pa pb = pathRecurse b [] pa pb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment