Time limit
2s
Memory limit
128 MB
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 move N cells in that direction. [direction] is one of Forward, Back, Left, and Right, and N is 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 of Forward, Back, Left, and Right. 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.
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.
Print the minimum number of commands required by the new robot.