In fact, he could also move east, it's the same sound value. He would go south because the code processes south before east. If the pit was rotated 90 degrees clockwise, we would have an endless loop:
West and south are both equivalent, but south is processed first, so the ogre goes to a dead end, moves back north one square to his initial position, but moves south again because it's processed before northwest. I guess it will be tricky to solve all pathfinding problems... for equal grids, you would have to guess the best path recursively by looking at the "next" steps and see which one would give the best result. This would ensure in this example that the ogre goes west to the open path instead of going south in a dead end.
Code:
.#.#. .#.#. .#..O .#.~. ##.#. ..@#. #####
Comment