Group C/C ++ A of the fourth Blue Bridge cup: revitalize China and blue bridge revitalize China

Source: Internet
Author: User

Group C/C ++ A of the fourth Blue Bridge cup: revitalize China and blue bridge revitalize China

First, paste the question!

James participated in the fun sports meeting of the school. One of the projects was to skip the grid.

There are some grids painted on the ground, and one word is written in each grid, as shown below: (see Figure 1)

Start from me
Start to revitalize
Start zhenxingzhong
Start to revitalize China

Figure 1

During the competition, you can jump to the adjacent grid either horizontally or vertically, but not to the diagonal grid or other positions, first standing in the grid with the word "from" in the upper left corner. It will always jump to the end of the word "Hua.

The line to be skipped is just like the phrase "start from me and revitalize China.

Could you help James calculate the total number of possible route jumps?

This question is quite simple, so in order to improve the difficulty, I added two more requirements:

1. All walking paths can be output!

2. A specific path can be output as required. (For example, this path: Starting from → starting from my workshop, then starting from Zhenzhen → xing→ zhong→hua)

 

OK. Let's briefly talk about my ideas:

First, set the word "start from me and revitalize China" to 0 ~ Compile the sequence of 7, store the square in a 4*5 Two-dimensional array, and set a flag array of the same size to store the walking track, finally, we need to set an array of road_flag [7] to record whether the walking step is horizontal or vertical.

    

Next, recursively traverse the two-dimensional array,

The recursion process is: Starting from 0, 0, moving forward either horizontally or vertically. The condition for moving forward is not beyond the range, and the number of the next grid is 1 larger than this grid. Each time you move forward to a grid, the corresponding position in the flag array is marked as 1, and the corresponding number of steps in road_flag is marked based on the direction of the walking step.

If it reaches the Chinese character (the corresponding number is 7), it will go to the recursive exit to determine whether the path meets the requirements, whether it can be output, and then return.

After the function is returned, clear the corresponding path tag and step tag.

 

In this way, traverse until all the paths are found!

 

The program code is as follows:

1 # include <stdio. h> 2 # include <string. h> 3 # define ROW 4 4 4 # define COL 5 5 6 int count; // count the number of paths 7 int flag [ROW] [COL]; // path marker 8 int road_flag [ROW + COL-1]; // step marker 9 int road_count; // records the number of walk steps 10 11 int road (int arr [] [COL], int row, int col); 12 13 int main (int argc, char * argv []) 14 {15 int array [ROW] [COL] = {16 {0, 1, 2, 3, 4}, 17 {1, 2, 3, 4 }, 18 {, 6}, 19 {, 7 }}; 20 21 road (array,); 22 printf ("count = % d \ n ", count); 23 24 return 0; 25} 26 int road (int arr [] [COL], int row, int col) 27 {28 flag [row] [col] = 1; // flag path 29 30 if (arr [row] [col] = 7) 31 {32 count ++; 33 // printf ("No. % d: \ n ", count ); 34 35 // determine whether this path meets our requirements 36 if (1 = road_flag [0] & 2 = road_flag [1] & 2 = road_flag [2] & 37 2 = road_flag [3] & 1 = road_flag [4] & 1 = road_flag [5] & 38 1 = road_flag [6] 39) 40 for (int rloop = 0; rloop <ROW; rloop ++) 41 {42 for (int cloop = 0; cloop <COL; cloop ++) 43 if (1 = flag [rloop] [cloop]) 44 printf ("#"); 45 else46 printf ("^"); 47 printf ("\ n "); 48} 49 return 0; 50} 51 // horizontal 52 if (col + 1 <COL & arr [row] [col + 1] = arr [row] [col] + 1) 53 {54 road_flag [road_count] = 1; // tag step 55 road_count ++; 56 57 road (arr, row, col + 1 ); 58 59 // cancel the path and step flag 60 flag [row] [col + 1] = 0; 61 road_count --; 62 road_flag [road_count] = 0; 63} 64 // vertical walk 65 if (row + 1 <ROW & arr [row + 1] [col] = arr [row] [col] + 1) 66 {67 road_flag [road_count] = 2; // tag step 68 road_count ++; 69 70 road (arr, row + 1, col ); 71 72 // cancel path and step mark 73 flag [row + 1] [col] = 0; 74 road_count --; 75 road_flag [road_count] = 0; 76} 77}

OK, the above program will be able to output a specific path according to our requirements (from → I'm doing something else, starting with Zhenzhen → xing→ 中→ Hua ), the count is the total number of paths! If you want to output all the paths, you only need to export the if Statement (36 ~ 39) remove it and cancel the annotation (33) of the printf statement above it to view all the paths!

The program running result is as follows:

  

The output results of all paths are as follows:

  

 

OK, That's all! Hope to help you!

Related Article

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.