This only works for two dimensional mazes where both the entrance and exit are on an edge and there are no loops in the maze.
If you introduce a bridge, or a tunnel, or put the goal in the middle, you can get stuck in a loop.