give you a maze. and drop a mouse in the maze randomly, and place a piece of cheese randomly. the mouse can move 4 directions
up down left right
There are some walls on the way to block certain directions for certain position.
design an algorithm for the mouse to find the cheese.
note, the mouse does not know where he is, and where the cheese is. he cannot find x,y for the position he is right now.
for instance
X X B
D X D
A X X
D is the block with walls on 4 sides.
If the mouse reaches the end of row or column, it gets the dead end.
Mouse is A, cheese is B. how to find the cheese.
Use dfs, and one stack and one hash table
Dfs traverse the maze, use stack to record the path, and hash table to record visited nodes
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment