Labyrinth problem (solving the problem when learning C++)