dp[i,j,k,v]表示当前坐标(i,j)下,拥有k件物品宝物最大价值为v时,方案的最大数。
i∈[1,M],j∈[1,N],用别人更新自己,最后输出的是dp[1,1,0,0]
调用dfs(1,1,0,-1),因为v表示当前取到的宝物的价值。判断都是用v和g[i,j]判断,但是dp更新或者传值都是用v+1
#include <stdio.h> #include <memory.h> #include <math.h> #include <string> #include <string.h> #include <vector> #include <set> #include <stack> #include <queue> #include <algorithm> #include <map> #define I scanf #define OL puts #define O printf #define F(a,b,c) for(a=b;a<c;a++) #define FF(a,b) for(a=0;a<b;a++) #define FG(a,b) for(a=b-1;a>=0;a--) #define LEN 100 #define MAX 0x06FFFFFF #define V vector<int> #define MOD 1000000007 typedef long long ll; using namespace std; int N,M,K; int g[LEN][LEN]; ll dp[LEN][LEN][15][15]; ll dfs(int i,int j,int k,int v){ ll s=0,t; if(dp[i][j][k][v+1]!=-1) return dp[i][j][k][v+1]; if(i==M && j==N){ if(k==K){ //不取 dp[i][j][k][v+1]=1; }else if(k==K-1 && g[i][j]>v){ //取 dp[i][j][k][v+1]=1; }else{ dp[i][j][k][v+1]=0; } return dp[i][j][k][v+1]; } t=g[i][j]; if(t>v){//取 if(i+1<=M) s+=dfs(i+1,j,k+1,t)%MOD; if(j+1<=N) s+=dfs(i,j+1,k+1,t)%MOD; } //不取 if(i+1<=M) s+=dfs(i+1,j,k,v)%MOD; if(j+1<=N) s+=dfs(i,j+1,k,v)%MOD; dp[i][j][k][v+1]=s%MOD; return dp[i][j][k][v+1]; } int main(){ // freopen("D:/CbWorkspace/blue_bridge/地宫取宝_1.txt","r",stdin); I("%d%d%d",&M,&N,&K); int i,j; F(i,1,M+1) F(j,1,N+1) I("%d",&g[i][j]); memset(dp,-1,sizeof dp); dfs(1,1,0,-1); O("%lld\n",dp[1][1][0][0]%MOD); return 0; }