#C409. Minimum Steps to Reach Destination

    ID: 47589 Type: Default 1000ms 256MiB

Minimum Steps to Reach Destination

Minimum Steps to Reach Destination

You are given a grid with m rows and n columns, where each cell represents an intersection. Starting from the top-left intersection (1, 1), your task is to determine the minimum number of steps required to reach the destination intersection (m, n). Some intersections are blocked and cannot be traversed. You may move only in the four cardinal directions: up, down, left, and right.

If the destination is unreachable, output \(-1\).

In an unobstructed grid, the minimum distance without obstacles is given by \(m+n-2\). However, with obstacles present, you must find the correct path using a suitable strategy such as breadth-first search (BFS).

inputFormat

The input is read from standard input (stdin) and follows this format:

  1. The first line contains two integers m and n, representing the number of rows and columns.
  2. The second line contains a single integer b, the number of blocked intersections.
  3. Each of the next b lines contains two integers x and y, representing the coordinates of a blocked intersection.

outputFormat

Output a single integer to standard output (stdout): the minimum number of steps required to get from the starting intersection (1, 1) to the destination (m, n). If it is impossible to reach the destination, output -1.

## sample
5 5
3
2 3
4 2
3 4
8