Treasures from the Underground Palace (Memory Search) Blue Bridge Cup Provincial Competition

Resource constraints

Time limit: 1.0s Memory limit: 256.0MB

Problem Description

King X has an underground treasury. is a matrix of nxm lattices. Put one treasure in each cell. Each baby has a value sticker attached.

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 only to the right or down.

When walking through a certain grid, if the value of the treasure in that grid is greater than that of any treasure in Xiao Ming's hand, Xiao Ming can pick it up (of course, he can also not take it).

When Xiaoming walks to the exit, if the treasures in his hand are exactly k, these treasures can be given to Xiaoming.

Please help Xiao Ming calculate how many different action plans he has to obtain these k treasures in a given situation.

input format

Enter a line of 3 integers, separated by spaces: nmk (1<=n,m<=50, 1<=k<=12)

Next, there are n rows of data, each row has m integers Ci (0<=Ci<=12) representing the value of the treasure on this grid

output format

It is required to output an integer, indicating that exactly the number of action plans for k treasures is taken. The number can be large, print it modulo 1000000007.

sample input

2 2 2
1 2
2 1

Sample output

2

sample input

2 3 2
1 2 3
2 1 5

Sample output

14

First, write a common search, and pay attention to two states when determining the exit:
1. When faced with a choice at the exit, if there are just k in hand, it is a solution (choose not to take it
at the exit) 2. When faced with a choice at the exit , if there are just k-1 items on hand, and the value of the exported treasure is greater than any treasure in the hand, it is also regarded as a solution (choose to take it at the exit)

To switch to memoization search, you only need to open a dimensional cache array as many as the dfs parameters, find the cache at the beginning of the search, record the cache at the end, and use the local variable ans to record and return in the middle.

#include<cstdio>
#include<iostream>
#include<cstring> 
using namespace std;
int mp[50][50];
long long cache[50][50][14][14];
int n,m,k;
long long dfs(int x,int y,int maxt,int con){
    
    
	long long ans=0;
	if(cache[x][y][maxt+1][con]!=-1)return cache[x][y][maxt+1][con];//历史上出现过 
	if(x==n||y==m)return 0;//出界 
	if(con>k)return 0;//剪枝 
	if(x==n-1&&y==m-1){
    
    //到了出口 
		if(con==k||(con==k-1&&mp[x][y]>maxt)){
    
    
			ans++;
			ans%=1000000007;
		}
		return ans;
	}
	if(mp[x][y]>maxt){
    
     //这个物品可以拿                     	
		ans+=dfs(x+1,y,mp[x][y],con+1);     	
		ans+=dfs(x,y+1,mp[x][y],con+1);     	
	}                                       	
		ans+=dfs(x+1,y,maxt,con);               	
		ans+=dfs(x,y+1,maxt,con);               	
	cache[x][y][maxt+1][con]=ans%1000000007;//记录状态	
	return ans%1000000007;

}
int main(){
    
    
	cin>>n>>m>>k;
	for(int i=0;i<n;i++){
    
    
		for(int j=0;j<m;j++){
    
    
			cin>>mp[i][j];
		}
	}
	memset(cache,-1,sizeof(cache));
	cout<<dfs(0,0,-1,0)<<endl;//起点处的物品价值可以等于0,所以初始状态最大子设-1 
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324980733&siteId=291194637