http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=642
//记忆化搜索 深搜
#include<algorithm> #include<cstdio> #include<cstring> using namespace std; int n,m,t,a,b; int data[101][101],dp[101][101][101]; int dfs(int i,int j,int left) { if(dp[i][j][left]!=-1) return dp[i][j][left]; if(i==m) { dp[i][j][left] = data[i][j]; return data[i][j]; } int ans = data[i][j]; if(left==0) ans+=dfs(i+1,j,left); else { int A=dfs(i+1,j,left); if(j>1) A=max(A,dfs(i+1,j-1,left-1)); if(j<n) A=max(A,dfs(i+1,j+1,left-1)); ans+=A; } dp[i][j][left] = ans; return ans; } int main(){ data[0][1]=0; int kase=1; while(scanf("%d%d%d",&n,&m,&t)!=EOF){ memset(dp,-1,sizeof(dp)); //特别注意这里不能用 0 初始化数组 因为dfs返回结果有大量0。。。。!!!!! memset(data,0,sizeof(data)); for(int i=1;i<=m;i++) { scanf("%d%d",&a,&b); data[i][a] += b; } printf("Scenario #%d\n",kase++); printf("%d\n\n",dfs(0,1,t)); } }