Flipkart
Online AssessmentLight-Cycle Racing Grid Simulation
Light-cycle racing takes place inside a rectangular grid. The lower-left corner of the grid is (0,0), and the upper-right corner is (W, D). Coordinates on the boundary are inside the grid.
Two players, A and B, start at different positions and move simultaneously. Both players initially face east, toward the positive x-axis.
Each player's path consists of movement segments in the form [T, k], where:
- T is 'S' (continue straight), 'L' (turn 90 degrees left), or 'R' (turn 90 degrees right).
- k is the number of grid units to move after turning.
A player moves one grid unit per second. Every coordinate occupied by a player becomes an impassable wall, including the starting coordinate.
A player dies when the player:
1. Moves outside the grid boundary [0, W] x [0, D];
2. Moves into a coordinate previously occupied by either player; or
3. Moves into the same coordinate as the other player during the same second.
Both players move simultaneously. If both players enter the same coordinate during a second, both players die.
The race ends immediately after a second in which at least one player dies. The race also ends at the current time when either player completes all movement segments (no additional movement is performed).
Print the time at which the race ends, followed by each player's final coordinate and status ('A' if alive, 'D' if dead).
- ▸Line 1: Two integers W and D (grid width and depth).
- ▸Line 2: Two integers xA and yA (Player A start coordinates).
- ▸Line 3: Integer nA, followed by nA movement segments for Player A (e.g., 'S 3 L 2 ...').
- ▸Line 4: Two integers xB and yB (Player B start coordinates).
- ▸Line 5: Integer nB, followed by nB movement segments for Player B.
- ▸Print the race end time, Player A's final (x, y) and status ('A' or 'D'), and Player B's final (x, y) and status ('A' or 'D'), separated by spaces:
- ▸`<time> <xA> <yA> <statusA> <xB> <yB> <statusB>`
- 1 <= W, D <= 1000
- 0 <= xA, xB <= W
- 0 <= yA, yB <= D
- (xA, yA) != (xB, yB)
- Total movement duration <= 10^5 seconds.
10 10 1 1 2 S 5 L 4 9 9 2 S 5 L 4
9 6 5 A 4 5 A
Both players execute their segments without colliding with walls, boundaries, or each other's trails. The race ends when movement segments complete.
1. Maintain player positions, headings (0: East, 1: North, 2: West, 3: South), and a set of visited/occupied grid coordinates. 2. Expand movement segments into step-by-step unit moves for both players. 3. At each second t: - Compute next intended coordinates for both players. - Evaluate boundary violations, trail collisions, and head-on collisions. - If any player dies, terminate race immediately, mark statuses, and print final coordinates. - If either player completes their plan with no collision, race terminates with both alive.