CSES Problem Labyrinth

The CSES Problem Labyrinth is a classic problem that can be solved using breadth-first search (BFS) algorithm. Here are the steps to solve this problem:

  1. First, read the input which consists of a grid representing the labyrinth. Each cell in the grid is either empty ('.') representing a passable path, or a wall ('#') representing an obstacle.

  2. Next, locate the starting point ('@') and the destination ('*') in the grid.

  3. Then, implement the BFS algorithm to find the shortest path from the starting point to the destination. The BFS algorithm explores all possible paths from the starting point and gradually expands to neighboring cells.

  4. During the BFS traversal, mark the visited cells to avoid revisiting them and keep track of the distance from the starting point to each cell.

  5. Continue the BFS traversal until the destination cell is reached, or until all reachable cells have been explored.

  6. Finally, output the length of the shortest path to the destination, or -1 if no path exists.

Implementing these steps will lead to a solution for the CSES Problem Labyrinth using the BFS algorithm.