[Blue Bridge Cup] Zhenti Training 2014 C++A Group Question 8: Taking treasure from the underground palace

Take treasure from the underground palace

King X has a treasure trove of underground palaces. Is a matrix of nxm grids. Put a baby in each grid. Each baby is affixed with a value label.
The entrance to the underground palace is in the upper left corner, and the exit is in the lower right corner.
Xiao Ming was taken to the entrance of the underground palace, and the king asked him to walk to the right or down.
When walking through a grid, if the value of the treasure in that grid is greater than that of any treasure in Xiao Ming's hands, Xiao Ming can pick it up (of course, you don't have to take it).
When Xiao Ming walks to the exit, if the treasures in his hand happen to be k pieces, these treasures can be given to Xiao Ming.
Please help Xiaoming calculate how many different action plans he has to obtain these k treasures in a given situation.
[Data format]
Enter 3 integers in a row, separated by spaces: nmk (1<=n,m<=50, 1<=k<=12)
Next, there are n rows of data, and each row has m integers Ci (0 <=Ci<=12) represents the value of the treasure on this grid. It is
required to output an integer, which means that the number of action plans for k treasures is taken. This number may be very large, and output the result of modulo 1000000007.
For example, input:
2 2 2
1 2
2 1 The
program should output:
2
Another example, input:
2 3 2
1 2 3
2 1 5 The
program should output:
14
Resource convention:
Peak memory consumption <256M
CPU consumption <1000ms
Please strictly follow Request output, don't superfluously print extra content like: "please enter...".
All the codes are placed in the same source file. After the debugging is passed, copy and submit the source code.
Note: The main function needs to return 0.
Note: Only use ANSI C/ANSI C++ standards and do not call special functions that depend on the compilation environment or operating system.
Note: All dependent functions must be #include <xxx> explicitly in the source file. Common header files cannot be omitted through project settings.
When submitting, pay attention to selecting the desired compiler type.

Problem analysis

Deep search + recursion + modulo

The recursive amount of change is each coordinate, the current maximum value, and the value of k (count);

Recursive call, when the current baby value is greater than the maximum value, then you can recurse down or to the right, and change the current maximum value to the current one, and the number of baby changes +1. If it does not exceed the current maximum value, then go straight down or to the right, and the number of items will not change;

Recursive exit, when the coordinate reaches the boundary, it can be returned. If the left side is adjusted to the last grid, then the number of baby changes needs to be considered. If the number of baby changes reaches the limit at this time, then return directly. Then you can proceed, you can +1 the result and then return.

Note that the question says that the result may be very large, so it is stored in longlong, and the specified number is taken modulo when returning.

#include <iostream>
using namespace std;

//深搜+递归+取模
int n, m, k;
int data[50][50];
long long res;
const int MOD = 1000000007;

void dfs(int x, int y, int max, int count){
	if(x == n || y == m){
		return ;
	}
		
	int cur = data[x][y];
	if(x == n-1 && y == m-1){
		if(count == k || (count == k-1 && cur > max)){
			res ++;
			if(res > MOD){
				res %= MOD;
			}
		}
	}
	
	if(cur > max){
		dfs(x, y+1, cur, count+1);
		dfs(x+1, y, cur, count+1);
	}

	dfs(x, y+1, max, count);
	dfs(x+1, y, max, count); 
}

int main(int argc, char** argv) {
	cin >> n >> m >> k;
	
	for(int i = 0; i < n; i ++){
		for(int j = 0; j < m; j++){
			cin >> data[i][j];
		}
	}
	
	dfs(0, 0, -1, 0);	//第一个点的价值为0; 
	 
	cout << res << endl;
	
	return 0;
}

 

Guess you like

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