#K71857. Shortest Path in Hazardous Grid

    ID: 33624 Type: Default 1000ms 256MiB

Shortest Path in Hazardous Grid

Shortest Path in Hazardous Grid

You are given a grid of size \(n \times m\) where each cell is identified by its coordinates \((x,y)\) with \(0 \leq x < n\) and \(0 \leq y < m\). Some intersections are hazardous and cannot be visited. Your task is to determine the minimum number of moves required to travel from the start cell at \((0,0)\) to the destination cell at \((n-1,m-1)\) while avoiding all hazardous cells. Moves are allowed only in four orthogonal directions: up, down, left, and right.

If either the starting point or the destination is hazardous or if there is no safe path available, output \(-1\).

inputFormat

The input is provided via standard input (stdin) in the following format:

 n m
 k
 x1 y1
 x2 y2
 ...
 xk yk

Here, \(n\) and \(m\) are the number of rows and columns of the grid, respectively. The integer \(k\) is the number of hazardous intersections. Each of the next \(k\) lines contains two integers \(x_i\) and \(y_i\) representing the coordinates of a hazardous intersection.

outputFormat

Output a single integer via standard output (stdout) which is the length of the shortest path from \((0,0)\) to \((n-1,m-1)\). If there is no valid path, output \(-1\).

## sample
5 5
2
2 2
3 3
8

</p>