#K51137. Remaining Robots
Remaining Robots
Remaining Robots
You are given a grid with cells indexed from 0 to 1000 in both directions. In each test case, N robots are placed on the grid at given coordinates. Each robot moves in a fixed direction: 'R' means it moves from its starting position to the right (i.e. increasing x‑coordinate) and 'C' means it moves upward (i.e. increasing y‑coordinate). A robot's path includes all grid cells from its starting coordinate up to and including cell 1000 in the moving direction.
If two or more robots visit the same grid cell (i.e. their paths intersect) they are considered to have collided and all vanish from the grid. Your task is to determine the number of robots that remain on the grid after processing all collisions for each test case.
Note: Each robot will vanish if it shares any common cell with at least one other robot along its path.
Input/Output Format: The input is read from standard input and the output is printed to standard output.
Formula: Two robots with starting coordinates \((x_1,y_1)\) and \((x_2,y_2)\) will have a collision if they satisfy one of the following conditions:
- Both are moving right (\(R\)) and are in the same row: \(y_1 = y_2\).
- Both are moving upward (\(C\)) and are in the same column: \(x_1 = x_2\).
- One moves right and the other upward, and they cross paths: for a robot moving right from \((x_1,y_1)\) and a robot moving upward from \((x_2,y_2)\), a collision occurs if \(x_2 \ge x_1\) and \(y_1 \ge y_2\).
inputFormat
The first line of input contains an integer T denoting the number of test cases. For each test case:
- The first line contains an integer N, the number of robots.
- Then follow N lines, each containing two integers x and y and a character d ('R' or 'C'), representing the starting x‑coordinate, y‑coordinate, and direction of the robot.
All input should be read from stdin
.
outputFormat
For each test case, output a single integer on a new line denoting the number of robots remaining after all collisions are resolved. All output should be written to stdout
.
4
3
1 2 R
3 4 C
1 4 R
2
1 1 R
2 2 C
3
0 0 R
0 0 C
0 0 R
4
1 2 C
2 2 R
2 3 R
1 3 C
1
2
0
2
</p>