The robot that bounces back
There is a room map with H rows and W columns.
Each cell of the map is . (empty), # (wall) or S (the empty cell where the robot starts), and there is exactly one S.
The robot follows a command string made of U (up), D (down), L (left) and R (right), one character at a time from the front.
For each command the robot moves one cell in that direction.
If the cell it moved to is outside the map or a wall, the robot returns to the cell it was on just before moving, and the bump count goes up by 1.
After all the commands, print the row number and column number of the robot's cell (both counted from 1) and the bump count on one line, separated by single spaces.
Try making the position a struct with two members, row and column, and a function (or method) that moves it one cell in place.
Before each move, save the position with prev = cur, and when blocked, restore it with cur = prev.
- Input
- The first line gives H and W, the next H lines give one row of the map each (length W, no spaces), and the last line gives the command string.
1 ≤ H, W ≤ 100, and the command string has length from 1 to 100,000. - Output
- One line: "row column bumps"
3 4 S..# .#.. .... RRRDDLLUR
2 1 2