#K79297. Minimum Moves on a Grid with Obstacles

    ID: 35277 Type: Default 1000ms 256MiB

Minimum Moves on a Grid with Obstacles

Minimum Moves on a Grid with Obstacles

You are given a destination point \( (X, Y) \) and an integer \(K\) representing the number of blocked positions on a 2D grid. Starting from \( (0, 0) \), you can move one unit at a time in the four cardinal directions: up, down, left, and right.

Your task is to determine the minimum number of moves required to reach the destination \( (X, Y) \) while avoiding the \(K\) blocked positions. If it is not possible to reach the destination, print \(-1\).

Note: The grid boundaries are defined as \(0 \leq x, y \leq 1000\). The obstacles are given as coordinates \( (B_x, B_y) \) and must be avoided during the traversal.

inputFormat

The input is read from standard input (stdin) and has the following format:

X Y K
B_x1 B_y1
B_x2 B_y2
... (K lines in total, each representing a blocked position)

Where:

  • \(X\) and \(Y\) are the coordinates of the destination.
  • \(K\) is the number of blocked positions.
  • Each of the following \(K\) lines contains two integers \(B_x\) and \(B_y\) which represent the blocked positions.

outputFormat

The output should be a single integer printed to standard output (stdout): the minimum number of moves required to reach \( (X, Y) \) from \( (0, 0) \). If the destination is unreachable, output \(-1\).

## sample
2 3 0
5