MJESEC
Time limit1sMemory limit512 MB
The robot moves on an unknown closed rectilinear track and reports its left and right turns after each move; find its final lattice position within 5000 moves.
- Level
Medium7 of 10
- Topics
- Geometry, Implementation, Simulation
- Solved
- No attempts yet
Problem
The fully automated lunar station's upkeep is the responsibility of the service robot M1RK8. The robot moves along a circular track (a track forming a cycle). The track consists of straight segments facing north, south, east, or west, and in-place 90-degree turns to the left or right. Each straight segment has a positive integer length in metres, so the whole track can be drawn on a Cartesian plane, with segments passing through lattice points (integer coordinates) and parallel to the axes. The track never intersects itself, and no two segments touch or overlap, except at adjacent endpoints (turns).
Here is an example track with 8 turns:

A meteorite struck the Moon near the station, and most sensors went offline. Neither the position of the robot nor the direction it faces is known. The station cannot begin repairs until we know the robot's position along the track.
The only command for moving the robot is move K, where K is a positive integer number of metres the robot must move along the track. After executing the command, the robot returns two integers, L and R. L is the number of left turns the robot made while executing the last move command, and R is the number of right turns.
Notes: The robot's starting position and its position after each command are always lattice points. If the robot ends a move command at a turn point, it turns toward the rest of the track before the command ends, so the next move command starts by going straight.
For example, if the robot is at (3, 3) facing east, then after 'move 4' it ends at (6, 2) facing north and returns '2 1', meaning it made two left turns and one right turn during the command. After that, 'move 1' moves the robot to (6, 3) and returns '0 0'.
Write a program that determines the robot's position using at most 5000 'move K' commands. The program must finish with the command 'stop X Y', where X and Y are the robot's final coordinates after executing all commands.
Limits
Time limit: 1000 ms. Memory limit: 512 MB.