Obstacle Course
Time limit1sMemory limit1024 MB
Find the minimum number of seconds to steer a sliding puck to a target on an ice rink, accelerating per second while avoiding integer-coordinate obstacles.
- Level
Hard8 of 10
- Topics
- BFS, Simulation, Implementation, Greedy
- Solved
- No attempts yet
Problem
You have a puck that slides across an ice rink. Once per second you may hit the puck from one side: from the north, south, east, or west. Hitting the puck from a side increases its speed in the opposite direction by meter per second. For example, a hit from the west increases the eastward speed by m/s, and a hit from the south increases the northward speed by m/s. Initially the puck rests at coordinates .
Example of movement: we hit the puck five times: from the west, from the south, from the west again, and then twice from the north. During the first second the puck moves meter east and reaches . During the second second it moves meter east and meter north, reaching . During the third second it moves meters east and meter north, reaching . During the fourth second it moves meters east, reaching . During the fifth second it moves meters east and meter south, reaching .
During each second the puck moves in a straight line from its start point to its end point. You are not required to hit the puck; if you do not, its direction and speed stay the same. The puck's maximum speed is m/s in each of the east-west and north-south directions. For example, if the puck is currently moving m/s west and m/s north, it can no longer be hit from the east, but it can still be hit from the other directions.
Obstacles are also placed on the ice. Each obstacle is an upright stick at a point with integer coordinates.
The goal is to move the puck to a given destination as quickly as possible without touching any obstacle. For simplicity, assume that both the sticks and the puck are dimensionless points, and that they touch each other if and only if they are at exactly the same point. The puck is said to touch an obstacle if it ends a second's path at a point that holds an obstacle, or if its path passes through such a point.
The puck does not need to stop at the destination, but it must finish that second's path exactly at the destination point.
Input
The first line contains three integers: the two coordinates of the destination and the number of obstacles (at most ). Each of the next lines contains two integers giving the position of one obstacle. All coordinates in the input are integers from to .
Output
Output exactly one integer: the minimum number of seconds needed to reach the destination. If the destination cannot be reached, output .
Hint
For the first sample, you can reach the destination in seconds by hitting the puck each second as follows: from the west, from the east, from the south, from the south, from the east.