NEFU 642 monkey(记忆化搜索)

82 篇文章 0 订阅
78 篇文章 0 订阅

description

 Cc is a lovely monkey. It likes to play the game "catching plates".
    The game is as follows.
    There are n pegs in a line numbered from 1 to n. Cc stands on the first peg at the beginning. It is rather hard for Cc to jump from peg i to peg i+1(i+1<=n) or peg i-1(i-1>=1), which takes him exactly one second. He can jump at most t times. And he will only jump at the beginning of one second.There is a clown at the other end. He has m plates in hand. He will throw a plate to one peg every second. The clown will throw the plate exactly to the peg. The plates will get broken if they hit the pegs. Cc can catch the plate if only he stands on the peg the plate is thrown to. For simplicity, the process of throwing and catching don't take any time at all. Suppose the clown will throw a plate to the 9th peg at the 9th second, if Cc stands on peg 9 at the 8th second and stands still for a second, or if Cc stand on peg 8 at the 8th second and jumps to peg 9, or if Cc stand on peg 10 at the 8th second and jumps to peg 9, he can catch the plate.
    There is a positive integer on each plate, indicating the bananas Cc can get if he catch that plate. Cc will know the way the clown throw the plates in advance. Now he wants to get as many bananas as possible.

							

input

 For each test case,the first line contains three integers n, m and t(1 <= n <= 100,1 <= m <= 100,0 <= t <= 100), indicating the number of the pegs, the number of the plates and the maximum number Cc can jump.The next m lines gives ai and bi each, which means the clown will throw a plate with number bi on it to peg ai at the ith second.
    Process to the end of file.
							

output

For each test case, first print a line saying "Scenario #k", where k is the number of the test case.Then, print the maximum number Cc can get, one per line.Print a blank line after each test case, even after the last one.
							

sample_input

4 4 2
2 10
3 15
1 10
1 8
4 4 4
2 10
3 15
1 10
1 8
2 2 1
1 1
2 1
							

sample_output

Scenario #1
28

Scenario #2
33

Scenario #3
2

记忆化搜索,效率比较高,据说按照常规dp还容易超时;

重要状态转移方程:  f[x][i][j] =max(f[x+1][i-1][j+1],f[x+1][i+1][j+1],f[x+1][i][j])+c[t][i]


代码:

#include <iostream>  
#include <cstring>  
#include <algorithm>  
  
using namespace std;  
  
int n,m,t;  
  
int a[111][111];  
int f[111][111][111];  
  
int dfs(int jp,int time,int place)  
{  
    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=0;  
    int maxt;  
    while (cin>>n>>m>>t)  
    {  
        cnt++;  
        maxt=0;  
        memset(f,-1,sizeof(f));  
        memset(a,0,sizeof(a));  
        for (int i=1; i<=m; i++)  
        {  
            int x,y;  
            cin>>x>>y;  
            a[i][x]+=y;  
        }  
        int ans=dfs(t,0,1);  
  
        cout<<"Scenario #"<<cnt<<endl;  
        cout<<ans<<endl<<endl;  
    }  
    return 0;  
}  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值