#K71862. Robot Movement Simulation
Robot Movement Simulation
Robot Movement Simulation
You are given a sequence of commands to control a robot on a 2D plane. The robot starts at the origin \((0,0)\) facing north. It accepts the following commands:
move X
: Move forward \(X\) units in the current direction.left
: Turn 90 degrees to the left, i.e., update the current direction as \(d = (d - 1) \mod 4\).right
: Turn 90 degrees to the right, i.e., update the current direction as \(d = (d + 1) \mod 4\).
The simulation proceeds by processing a list of commands in order. The robot does not move when turning. The final position should be output as the coordinates \(x\) and \(y\) separated by a space.
The input consists of multiple test cases. For each test case, the first line is an integer \(N\) representing the number of commands, followed by \(N\) lines each containing a command. The sequence terminates when a test case with \(N = 0\) is encountered. For each test case, print the final \(x\) and \(y\) coordinates on a separate line.
inputFormat
The input is read from standard input (stdin) and consists of multiple test cases. Each test case has the following format:
N command_1 command_2 ... command_N
Here, \(N\) is a positive integer representing the number of commands. The commands can be:
move X
(whereX
is a positive integer)left
right
The input terminates with a line containing a single 0
, which should not be processed.
outputFormat
For each test case, output a single line containing two integers separated by a space: the final \(x\) and \(y\) coordinates of the robot after executing all commands.
## sample3
move 2
right
move 3
0
3 2
</p>