Questions from over the years (dice game 7)

Source: Internet
Author: User
  • Problem description:

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

  • Example

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 ;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.