题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=642
记忆化搜索,状态转移公式很好找
dp[time][place][t]
=max{dp[time-1][place-1][t-1],dp[time-1][place][jp],dp[time-1][place+1][jp-1]};
time表示所用的时间,place表示所在的位置jp表示所剩的跳跃次所用的跳跃次数;
dp[time][place][t]表示在time时间落在place上跳了t次后 所得到的最多的香蕉。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int f[120][120][120];
int a[120][120];
int n,m,t;
int dfs(int jp,int time,int place)//time表示时间,place表示所在的位置,jp表示所剩的跳跃次数
{
if (f[jp][time][place]!=-1) return f[jp][time][place];
int ret=0;
if (time<m) ret=dfs(jp,time+1,place);
if (place+1<=n&&jp>0&&time<m)
{
ret=max( ret, dfs(jp-1,time+1,place+1) );
}
if (place-1>=1&&jp>0&&time<m)
{
ret=max( ret, dfs(jp-1,time+1,place-1) );
}
f[jp][time][place]=ret+a[time][place];
return f[jp][time][place];
}
int main()
{
int cnt=1;
while(cin>>n>>m>>t)
{
memset(f,-1,sizeof(f));
memset(a,0,sizeof(a));
int x,y;
for(int i=1;i<=m;i++)
{
cin>>x>>y;
a[i][x]+=y;
}
cout<<"Scenario #"<<cnt++<<endl;
cout<<dfs(t,0,1)<<endl<<endl;
}
return 0;
}