The easiest to code and to understand would be a flood fill I guess. (Well... perhaps a simple recursion would be easier to understand). Anyway, here is my explanation of flooding this maze by example:
N = 0
1) See what you can reach in N steps without using a square double.
2) If you can reach the end point, it must be shortest.
3) Otherwise: increase N.
- Code: Select all
###### ###### ###### ####### ###### ######
@ # -> 1@1 # -> 1@12 # -> 1@12 # -> 1@123# -> 1@123#
# # ! #1 # ! #12# ! #12# ! #12#4! #12#45 <- found it.
### # ### # ### # ### # ### # ### 5#
For the implementation, I advise a queue. Pseudo code:
1) push the starting point in the (back of the) queue.
2) while queue_not_empty repeat
3) get the position in front of the queue ("pop")
4) if the front is end point, you are done (leave while)
5) mark this spot.
6) push all neighbours that are possible (i.e. inside map, not wall, not marked etc)
If you did not find an endpoint, the queue will drain and become empty (because there are no more spaces that are possible). There seems to be no solution.
If you find a endpoint, you have found
just 1 shortest route. There may be other that are equally fast. But there can't be a route that is faster (no proof given here).
humor mode:
See my code for an example
