#C6584. Minimum Steps on Obstructed Grid
Minimum Steps on Obstructed Grid
Minimum Steps on Obstructed Grid
You are given a two-dimensional grid and a target point \((n, m)\). Starting at \((0, 0)\), you may move one step at a time in any of the four cardinal directions (up, down, left, right). Certain grid points are blocked and cannot be passed through.
Your task is to determine the minimum number of steps required to reach \((n, m)\) without stepping on any of the blocked points. If the destination is unreachable, print \(-1\).
Recall that in an unobstructed grid the Manhattan distance from \((0, 0)\) to \((n, m)\) is \(n+m\), which would be the minimum number of steps.
inputFormat
The input is read from standard input and has the following format:
n m k a1 b1 a2 b2 ... ak bk
Where:
- \(n\) and \(m\) are the coordinates of the destination point \((n, m)\).
- \(k\) is the number of blocked points.
- Each of the next \(k\) lines contains two non-negative integers \(a_i\) and \(b_i\), which represent a blocked grid point.
outputFormat
Output a single integer to standard output: the minimum number of steps required to reach ((n, m)) from ((0, 0)) while avoiding blocked points. If there is no valid path, output (-1).## sample
3 4
2
1 2
2 3
7