Fourth session Blue Bridge Cup c/c++a Group title: Rejuvenating China

Source: Internet
Author: User
Tags ming

First, put the title on it!

Xiao Ming took part in the school fun Games, one of which is: jump lattice.

Some squares are drawn on the ground, one word in each lattice, as follows: (also see figure 1)

From the start I do the vibration
I do the revitalization
Do the revitalization
From rejuvenating China

Figure 1

In the game, standing in the upper left corner of the "from" box, you can jump horizontally or vertically to the adjacent lattice, but not to jump to the diagonal lattice or other positions. Have to jump to the end of the word "Hua".

The route required to skip is just the "rejuvenating China from me" sentence.

Please help Xiao Ming calculate how many kinds of possible jumping routes he has altogether?

This topic is quite simple, so in order to improve the difficulty, then I added two additional requirements:

1. Be able to output all the walking paths!

2. Ability to output a specific path as required. (such as this path: from → I ↓ to do ↓ from ↓ → hing → Chinese → China)

OK, simply say my thoughts:

First of all, "from me, rejuvenating China" The eight words in accordance with the order of 0~7, and then put this box in a 4*5 two-dimensional array of arrays, at the same time, set a flag array of the same size to store the walking trajectory, and finally set a road_flag[7] Array to record whether the walking steps are horizontal or vertical.

    

The next step is to traverse the two-dimensional array recursively,

The recursive process is: starting from 0, 0, sideways or vertically forward, the condition of moving forward one bar is not out of range, and the next lattice of the number than this lattice 1. After each advance, the corresponding position in the flag array is marked as 1, and the corresponding steps in the Road_flag are marked according to the direction of the walking steps.

If you reach the Chinese Word (the corresponding number is 7), then to the recursive exit, determine whether this path meets the requirements, whether it can output, and then return.

After the function returns, the corresponding path mark and the footstep mark are cleared.

Just keep traversing until you find all the paths!

The code for the program is as follows:

1#include <stdio.h>2#include <string.h>3 #defineROW 44 #defineCOL 55 6 intCount//Number of statistics paths7 intFlag[row][col];//Path Marker8 introad_flag[row+col-1];//Footstep Markers9 intRoad_count;//the number of steps used to record a walkTen  One intRoadintArr[][col],intRowintcol); A  - intMainintargcChar*argv[]) - { the     intArray[row][col] = { -{0,1,2,3,4}, -{1,2,3,4,5}, -{2,3,4,5,6}, +{3,4,5,6,7}   }; -  +Road (Array,0,0); Aprintf"count =%d\n", count); at  -     return 0; - } - intRoadintArr[][col],intRowintCol) - { -flag[row][col]=1;//Mark Path in  -     if(Arr[row][col] = =7) to     { +count++; -     //printf ("no.%d:\n", count); the  *     //determine if this path meets our requirements $     if(1==road_flag[0] &&2==road_flag[1] &&2==road_flag[2] &&Panax Notoginseng        2==road_flag[3] &&1==road_flag[4] &&1==road_flag[5] && -        1==road_flag[6] the     ) +          for(intrloop=0; rloop<row;rloop++) A         { the          for(intcloop=0; cloop<col;cloop++) +         if(1==Flag[rloop][cloop]) -printf" # "); $         Else $printf" ^ "); -printf"\ n"); -         } the     return 0; -     }Wuyi     //Transverse walk the     if(col+1<col && arr[row][col+1]==arr[row][col]+1) -     { WuRoad_flag[road_count] =1;//Marking Steps -road_count++; About  $Road (arr,row,col+1);  -  -     //canceling paths and footstep markers -flag[row][col+1] =0; Aroad_count--; +Road_flag[road_count] =0; the     } -     //Longitudinal Walk $     if(row+1<row && arr[row+1][col]==arr[row][col]+1) the     { theRoad_flag[road_count] =2;//Marking Steps theroad_count++; the  -Road (arr,row+1, col); in  the     //canceling paths and footstep markers theflag[row+1][col] =0; Aboutroad_count--; theRoad_flag[road_count] =0; the     } the}

Ok, the above program will be able to follow our request to output a specific path (from → I ↓ to do ↓ ↓ vibration → xing → in → China), and that count is a total number of paths! If you want to output all the paths, simply remove the IF statement (36~39) from the recursive exit and remove the note (33) from the printf statement above it to see all the paths!

The running results of the program are as follows:

  

The result of outputting all paths is as follows:

  

Ok,that ' s all! Hope to be able to help everyone!

Fourth session Blue Bridge Cup c/c++a Group title: Rejuvenating China

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.