#P2873. Clean Boots Journey
Clean Boots Journey
Clean Boots Journey
Farmer John needs to reach his cow, Bessie, while avoiding muddy puddles in the field to keep his boots clean. He starts at (0,0) and must travel to (X,Y). He can only move in steps parallel to the axes (up, down, left, right) and can only change direction at points with integer coordinates. The distance traveled is measured in Manhattan distance, given by \(d = |x_1 - x_2| + |y_1 - y_2|\). Some coordinates are occupied by puddles (muddy spots) that he must avoid stepping on. You are guaranteed that there is always a valid path that avoids all puddles.
Your task is to compute the shortest distance Farmer John must travel from (0,0) to (X,Y) while avoiding all coordinates that have puddles.
inputFormat
The first line contains two integers X
and Y
, representing Bessie's coordinates (-500 ≤ X, Y ≤ 500).
The second line contains an integer N
(1 ≤ N ≤ 10,000), the number of puddles.
Each of the following N
lines contains two integers Ai
and Bi
(-500 ≤ Ai, Bi ≤ 500), which represent the coordinates of a puddle. Each puddle occupies exactly one point.
outputFormat
Output a single integer: the minimum distance Farmer John must travel to reach Bessie while avoiding all puddles.
sample
2 2
1
1 1
4