Robot Commands
Time limit2sMemory limit128 MB
Convert an old robot's forward/turn/scan command sequence into a minimal equivalent sequence for a new robot with directional move and scan commands producing the same scanned cells in order.
- Level
Medium6 of 10
- Topics
- Simulation, Greedy, Implementation
- Solved
- No attempts yet
Problem
An old exploration robot understands four commands:
Forward: move one cell forward.Turn Left: rotate 90 degrees to the left.Turn Right: rotate 90 degrees to the right.Scan: inspect the cell directly in front of the robot.
The robot works on a square grid, so every rotation is exactly 90 degrees. Turning around therefore requires two rotation commands.
A new robot supports only the following two command forms:
Move [direction] [N]: rotate toward[direction], then moveNcells in that direction.[direction]is one ofForward,Back,Left, andRight, andNis a positive integer. After moving, the robot keeps facing the direction in which it moved.Scan [direction]: rotate toward[direction], then inspect the adjacent cell in that direction.[direction]is one ofForward,Back,Left, andRight. After scanning, the robot keeps facing the direction it scanned.
Given the command sequence executed by the old robot, find the minimum number of commands needed for the new robot to inspect the same cells in the same order.
The old and new robots start at the same position and face the same direction. The new robot's intermediate path may differ from the old robot's path; only the inspected cells and their order must match. If the old command sequence inspects the same cell multiple times, the new command sequence must also inspect that same cell the same number of times. The robot may pass through cells that will be inspected later.
Input
The first line contains the number of commands N performed by the old robot (1 <= N <= 100,000).
Each of the next N lines contains one command in the old robot's format.
Output
Print the minimum number of commands required by the new robot.