Centennial Woodpecker
Time limit2sMemory limit512 MB
A ship moves right and can only turn right; stars block it. Count the energy spent before a coordinate exceeds 1e10, or report that it never happens.
- Level
Medium6 of 10
- Topics
- Simulation, Implementation, Hash map, Math
- Solved
- No attempts yet
Problem
The map of a galaxy far, far away is an infinite plane divided into unit squares. Some squares contain stars, and flying through them is dangerous. The remaining squares are safe.
The spaceship <> emerges from a wormhole in the square and initially moves to the right (that is, in the direction of increasing first coordinate). After a hard battle the ship's engine is damaged, so the ship can only turn right by a right angle. The ship is controlled by an autopilot, which, if the next square along the current course is safe, moves the ship into it without spending energy. Otherwise the autopilot stays in the current square and turns, spending one unit of energy to do so.

You must determine how many units of energy the ship will spend until one of its coordinates exceeds in absolute value, or determine that this will never happen.
Input
The first line of input contains an integer , the number of stars in the galaxy ().
Each of the following lines contains two integers and , the coordinates of one star (). It is guaranteed that no two stars occupy the same square and that the square contains no star.
Output
Print a single number, the number of units of energy the ship will spend during its journey if the journey ends, or <<oo>> if it never ends.