#K40827. Shortest Path in a Grid

    ID: 26729 Type: Default 1000ms 256MiB

Shortest Path in a Grid

Shortest Path in a Grid

You are given a 2D grid with N rows and M columns. Some cells in the grid are blocked. Your task is to find the shortest path from the top-left corner (cell ( (0,0) )) to the bottom-right corner (cell ( (N-1, M-1) )) using only moves in the four cardinal directions (up, down, left, right). The blocked cells cannot be traversed. If no valid path exists, output ( \texttt{IMPOSSIBLE} ).

Note: The grid indices are 0-indexed. Use breadth-first search (BFS) or any other appropriate algorithm to solve this problem efficiently.

inputFormat

The input is read from standard input (stdin). The first line contains three integers ( N ), ( M ), and ( K ) separated by spaces, representing the number of rows, number of columns, and the number of blocked cells respectively. This is followed by ( K ) lines, each containing two integers representing the row and column indices of a blocked cell.

outputFormat

Output the length of the shortest path from the top-left cell to the bottom-right cell in the grid as an integer. If no such path exists, print ( \texttt{IMPOSSIBLE} ). The output should be written to standard output (stdout).## sample

4 4 3
1 1
2 2
3 1
6