#P9465. Dustbot Navigation
Dustbot Navigation
Dustbot Navigation
A vacuum robot starts at the top-left corner (0,0) of a basement which is represented by an \(H \times W\) grid. The rows are numbered from \(0\) to \(H-1\) (top to bottom) and the columns from \(0\) to \(W-1\) (left to right). There is a treasure hidden in a box in some unknown cell (different from \((0,0)\)), but the only way to locate it is to remotely control the robot via a set of instructions.
Each evening, the robot is given a string of commands consisting of the characters <
, >
, ^
and v
. Formally, if the robot is at cell \((r, c)\) then:
<
moves the robot left to \((r, c-1)\) if possible.>
moves the robot right to \((r, c+1)\) if possible.^
moves the robot up to \((r-1, c)\) if possible.v
moves the robot down to \((r+1, c)\) if possible.
If executing a command would move the robot out of the grid, the robot remains in its current cell for that command. At the end of the evening, the robot reports its final position and then returns to the starting cell \((0,0)\) for the next round.
Your task is to simulate the movement of the robot based on the provided instructions and output its final position.
inputFormat
The input consists of two lines:
- The first line contains two integers \(H\) and \(W\) representing the dimensions of the grid.
- The second line contains a non-empty string composed of the characters
<
,>
,^
, andv
representing the movement instructions.
outputFormat
Output the final row and column coordinates of the robot (separated by a space) after processing all the commands.
sample
3 3
v>v
2 1