Problem
Given a n x m two-dimensional array board and a list of robot commands queries, write a function solution that calculates the total score obtained by the robot as it follows the commands. The robot starts at the top-left corner of the board.
The commands in queries include the following:
Move Forward: Moves the robot 1 step in the direction it is facing. If moving is not possible, the robot stays in the same position.Move Backward: Moves the robot 1 step in the opposite direction it is facing. If moving is not possible, the robot stays in the same position.Turn Left: Rotates the robot's direction 90 degrees counterclockwise. The robot's position does not change.Turn Right: Rotates the robot's direction 90 degrees clockwise. The robot's position does not change.Get Score: Adds the value of the current cell to the total score. The robot's position does not change.
Constraints
- The dimensions of
board(nandm) are between 2 and 20 inclusive.nandmcan be different. - Each element
board[i][j]is a natural number between 1 and 1000 inclusive. - The length of
queriesis between 5 and 1000 inclusive. - Each element of
queriesis one ofMove Forward,Move Backward,Turn Left,Turn Right,Get Score. - The
querieslist contains at least oneGet Scorecommand.
Test Case
| Test Case Number | board | queries | return |
|---|---|---|---|
| 1 | [[4,7,18,20],[11,16,1,4],[1,16,7,12]] | ["Move Backward","Move Forward","Get Score","Turn Left","Move Forward"] | 11 |
| 2 | [[15,1,6],[4,6,17],[2,16,17],[18,8,8],[5,17,10]] | ["Get Score","Move Forward","Move Forward","Turn Left","Move Backward","Get Score","Get Score","Move Forward","Get Score"] | 35 |
| 3 | [[5,16,9],[8,18,5],[1,1,15],[20,18,16],[19,4,15]] | ["Turn Right","Move Forward","Get Score","Turn Right","Turn Right","Get Score","Move Backward","Turn Right","Get Score"] | 15 |
Explanation for Test Case 1
| query | Robot Position (row, col) | Robot Direction | Total Score |
|---|---|---|---|
| - | (0, 0) | ↓ | 0 |
| Move Backward | (0, 0) | ↓ | 0 |
| Move Forward | (1, 0) | ↓ | 0 |
| Get Score | (1, 0) | ↓ | 11 |
| Turn Left | (1, 0) | → | 11 |
| Move Forward | (1, 1) | → | 11 |
Explanation for Test Case 2
| query | Robot Position (row, col) | Robot Direction | Total Score |
|---|---|---|---|
| - | (0, 0) | ↓ | 0 |
| Get Score | (0, 0) | ↓ | 15 |
| Move Forward | (1, 0) | ↓ | 15 |
| Move Forward | (2, 0) | ↓ | 15 |
| Turn Left | (2, 0) | → | 15 |
| Move Backward | (2, 0) | → | 15 |
| Get Score | (2, 0) | → | 17 |
| Get Score | (2, 0) | → | 19 |
| Move Forward | (2, 2) | → | 19 |
| Get Score | (2, 2) | → | 35 |
Explanation for Test Case 3
| query | Robot Position (row, col) | Robot Direction | Total Score |
|---|---|---|---|
| - | (0, 0) | ↓ | 0 |
| Turn Right | (0, 0) | ← | 0 |
| Move Forward | (0, 0) | ← | 0 |
| Get Score | (0, 0) | ← | 5 |
| Turn Right | (0, 0) | ↑ | 5 |
| Turn Right | (0, 0) | → | 5 |
| Get Score | (0, 0) | → | 10 |
| Move Backward | (0, 0 |