[Blue Bridge Cup] Exam Training 2013 C++A Group Question 3 Revitalize China

Revitalize China 

 Revitalize China

    Xiao Ming participated in the school's fun sports meet, and one of the events was: skipping the grid.
    There are some grids drawn on the ground, and a word is written in each grid, as shown below: (also see p1.jpg)

Starting from me, starting from me, starting
from revitalizing,
starting from revitalizing,
starting from revitalizing China  

    During the game, first stand in the grid with the word "from" in the upper left corner. You can jump to the adjacent grid horizontally or vertically, but not to the diagonal grid or other positions. Always jump to the end of the word "Hua".

    The route requested to be skipped just constitutes the phrase "starting from me to revitalize China."

    Please help Xiaoming calculate how many possible jumping routes he has in total?

The answer is an integer, please submit the number directly through the browser.
Note: Do not submit the answering process or other supporting explanation content.

Answer: 35

 

Problem analysis

enumerate

Recursion, iteration 

Recursively grasp the three main points: find the change state (parameter), repeated steps (recursive call), exit (end condition)

In this question, every step to the right or down is okay, and the new position reached each time is also down or right, then this is a repeated step, the place of change is every time the coordinate changes, then exit That is, when the abscissa or ordinate reaches the boundary, it reaches the exit and can end the return.

#include <iostream>
using namespace std;
//振兴中华

int f(int x, int y){	//状态
	//出口 
	if(x == 3 || y == 4){
		return 1;
	} 
	return f(x+1, y) + f(x, y+1);	//重复 
} 

int main(int argc, char** argv) {
	cout << f(0, 0) << endl; 
	return 0;
}

 This problem is not difficult. It is easy to understand using this recursive approach to solve the problem. Don't panic when you see the problem. In the Blue Bridge Cup competition, this kind of grid problem is very common. Come on!

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/115088112