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 Up: Moves the robot 1 step up. If moving up is not possible, the robot stays in the same position.Move Down: Moves the robot 1 step down. If moving down is not possible, the robot stays in the same position.Move Left: Moves the robot 1 step left. If moving left is not possible, the robot stays in the same position.Move Right: Moves the robot 1 step right. If moving right is not possible, the robot stays in the same position.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 Up,Move Down,Move Left,Move Right,Get Score. - The
querieslist contains at least oneGet Scorecommand.
Test Case
| Test Case Number | board | queries | return |
|---|---|---|---|
| 1 | [[10,20,16],[8,6,11],[10,2,12]] | ["Move Left","Move Right","Get Score","Move Right","Move Down"] | 20 |
| 2 | [[13,1,13,8,16],[4,7,20,16,5],[7,3,4,10,13],[9,4,2,15,13]] | ["Move Down","Get Score","Move Down","Move Up","Move Right","Move Right","Move Down","Move Up","Get Score"] | 24 |
| 3 | [[1,8,14],[8,17,13],[7,12,9],[5,13,19]] | ["Get Score","Get Score","Move Down","Move Down","Move Down"] | 2 |
Explanation for Test Case 1
| query | Robot Position (row, col) | Total Score |
|---|---|---|
| - | (0, 0) | 0 |
| Move Left | (0, 0) | 0 |
| Move Right | (0, 1) | 0 |
| Get Score | (0, 1) | 20 |
| Move Right | (0, 2) | 20 |
| Move Down | (1, 2) | 20 |
Explanation for Test Case 2
| query | Robot Position (row, col) | Total Score |
|---|---|---|
| - | (0, 0) | 0 |
| Move Down | (1, 0) | 0 |
| Get Score | (1, 0) | 4 |
| Move Down | (2, 0) | 4 |
| Move Up | (1, 0) | 4 |
| Move Right | (1, 1) | 4 |
| Move Right | (1, 2) | 4 |
| Move Down | (2, 2) | 4 |
| Move Up | (1, 2) | 4 |
| Get Score | (1, 2) | 24 |
Explanation for Test Case 3
| query | Robot Position (row, col) | Total Score |
|---|---|---|
| Get Score | (0, 0) | 1 |
| Get Score | (0, 0) | 2 |
| Move Down | (1, 0) | 2 |
| Move Down | (2, 0) | 2 |
| Move Down | (3, 0) | 2 |