Moving Robots
Time limit1sMemory limit128 MB
Given several robots each with a bounded command sequence, find the minimum total deletions so all robots can be made to stop at one common grid cell, breaking ties by lexicographically smallest position.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Simulation, Graph
- Solved
- No attempts yet
Problem
Several robots move on an infinite two-dimensional integer grid. Each robot has a current position and a facing direction, and moves by executing its own finite sequence of commands in order.
A position is a pair of integers (x, y). A direction is one of 0, 90, 180, or 270 degrees.
There are two kinds of commands:
- Turn, with a parameter D ∈ {90, 180, 270}: the current direction C becomes (C + D) mod 360.
- Step, with no parameter: the robot moves one cell in its current direction. A step in direction 0 changes the position by (1, 0); direction 90 by (0, 1); direction 180 by (-1, 0); direction 270 by (0, -1).
A robot executes its commands one after another and stops at the final position after the last command. Robots do not affect one another, and any number of robots may occupy the same cell.
Before the robots start, you may delete any subset of commands from any robot's sequence (the remaining commands are executed in their original order). Deleting commands changes where each robot stops. Your goal is to make every robot stop at one common final position, using the minimum possible total number of deleted commands over all robots.
There are R robots (2 ≤ R ≤ 10). Each robot has its own initial position and direction and a command sequence of at most 50 commands. If possible, determine:
- the minimum total number of commands that must be deleted so that all robots stop at the same final position;
- that common final position.
If several positions achieve this minimum, output the lexicographically smallest one: the position with the smallest x, and among those the smallest y.
Input
The first line contains the integer R (2 ≤ R ≤ 10), the number of robots. Then follow R robot descriptions.
Each description begins with a line of four space-separated integers x y C n: the robot's initial position (x, y), its initial direction C (C ∈ {0, 90, 180, 270}), and the number of commands n (1 ≤ n ≤ 50). The next n lines list the commands, one per line. A step command is the single character S; a turn command is the character T followed by a single space and the integer D (D ∈ {90, 180, 270}).
Output
If it is impossible to make all robots stop at a common final position by deleting commands, print a single line containing -1.
Otherwise print two lines: the first line contains the minimum total number of deleted commands; the second line contains two space-separated integers, the x and y coordinates of the common final position. If several positions achieve the minimum, print the lexicographically smallest one (smallest x; if tied, smallest y).