#P8070. Synchronizing Robots
Synchronizing Robots
Synchronizing Robots
There are multiple robots on a 2D plane, all starting at the origin ((0,0)) with an initial direction of (0^\circ). Each robot is given a sequence of commands, and the commands are of two types:
- S: Move one step forward in the current direction. In detail, if the robot is at \((x,y)\) and its current direction is \(C\), then:
- If \(C = 0^\circ\), it moves to \((x+1,y)\).
- If \(C = 90^\circ\), it moves to \((x,y+1)\).
- If \(C = 180^\circ\), it moves to \((x-1,y)\).
- If \(C = 270^\circ\), it moves to \((x,y-1)\). - T D: Rotate the robot. This command increases the current direction by \(D\) degrees, i.e. update \(C \gets (C+D) \bmod 360\). Note that \(D\) can be any integer.
You are allowed to remove any number of commands (from any robot’s sequence) while keeping the relative order of the remaining commands intact. The goal is to remove commands so that after executing the remaining commands, all robots finish at the same position (their final orientations do not matter). Furthermore, you wish to minimize the total number of commands removed across all robots.
Mathematically, if robot (i) originally has (k_i) commands and you choose a subsequence of commands (maintaining order) yielding (r_i) accepted commands so that the final position of every robot is identical, your answer is the minimum total removals given by (\sum_{i} (k_i - r_i)).
inputFormat
The input begins with a single integer (n) (the number of robots). Then for each robot, there is a block of input:
k command_1 command_2 ... command_k
where (k) is the number of commands for that robot. Each command is either:
- S
- T D — where D is an integer
All commands are separated by line breaks.
outputFormat
Output a single integer representing the minimum total number of commands that must be removed so that all robots finish at the same position.
sample
2
3
S
T 90
S
3
S
T 270
S
4