In the dice game, a few steps forward in the map will be taken based on the number you throw. After the progress is completed, you need to perform corresponding operations based on the obstacles shown in the current map position, where the obstacles are:
1) 9: Accessibility
2) 1. Stop throwing a round, that is, the number of the next round is invalid;
3) 2: Two steps back. If you have reached the starting point, you will not be back;
4) 3: reward for the next step
If you have reached the map endpoint during the game, the game is over. Based on the input map array and the array of five dice, return how many steps the final player has taken.
- Required implementation functions:
Void dice (INT map_len, int * map, int * dice_val, int * output)
[Input] int map_len, the length of the map Array
Int * map, map array, value indicates Obstacle
Int * dice_val, an array of five dice
[Output] int * output, the total number of steps forward by players
[Return] None
Note: players start from the starting position, that is, the first place in the map array. The number of dice can only be 1 ~ 6
1) input: map_len = 15, map = {9, 9, 9, 9, 9, 9, 9}, dice_val = {1, 1, 3, 1 },
Return Value: 4
2) input: map_len = 16, map = {9,9, 9,9, 9,1, 9,3, 9,9, 9,9, 9,9}, dice_val = {, 6, 6 },
Return Value: 15
# Include <iostream> using namespace STD; void dice (INT Len, int * map, int * dice_val, int * output) {int I = 0, j = 0, step = 0; For (; I <Len & J <5;) {step = dice_val [J ++]; I + = step; if (I> = len-1) // if the game has reached the map endpoint, the game is over. {* Output = len-1; return;} If (Map [I] = 9) continue; else if (Map [I] = 1) J ++; else if (Map [I] = 2) {If (I-2> = 0) I-= 2; else I = 0 ;} else if (Map [I] = 3) {I ++; if (I> = len-1) // if you have reached the map endpoint during the game, the game ends. {* Output = len-1; return ;}}* output = I;} void main () {int map_len = 16; int map [16] = {9, 9, 9, 9, 1, 9, 3, 9, 9, 9, 9, 9}; int dice_val [5] = {, 6}; int * output = new int; dice (map_len, MAP, dice_val, output); cout <* output; cout <Endl ;}