Monkey CC | ||||||
| ||||||
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 |
题目大意:
这个场地有n个格子,初始的时候站在1号格子里边,一共这个游戏有m秒,每一秒小丑都会在位子ai扔出一个价值为bi的物品,如果我这一秒在这,就能获得这个价值,我一秒能够移动一个格子,而且做多只能移动t次,问最大价值。
思路;
1、设定dp【i】【j】【k】表示在时间i的时候 ,处于位子j,还可以移动k次的最大值。
2、辣么不难理解其状态转移方程:
dp【i】【j】【k】=max(dp【i+1】【j】【k】,dp【i+1】【j-1】【k-1】,dp【i+1】【j+1】【k-1】);
if(pos[i]==j)dp【i】【j】【k】+=val【i】;
3、然后我们使用记忆化搜索的方式枚举所有解,其时间复杂度:O(N*M*K);
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int dp[105][105][105];
int pos[105];
int val[105];
int n,m,t;
int Dfs(int nowtime,int poss,int tt)
{
// printf("%d %d %d\n",nowtime,poss,tt);
if(tt>t||tt<0)return 0;
if(poss<1||poss>n)return 0;
if(nowtime>m)return 0;
if(dp[nowtime][poss][tt]==-1)
{
int c=Dfs(nowtime+1,poss+1,tt-1);
int a=Dfs(nowtime+1,poss,tt);
int b=Dfs(nowtime+1,poss-1,tt-1);
dp[nowtime][poss][tt]=max(max(a,b),c);
if(pos[nowtime]==poss)dp[nowtime][poss][tt]+=val[nowtime];
return dp[nowtime][poss][tt];
}
else return dp[nowtime][poss][tt];
}
int main()
{
int kase=0;
while(~scanf("%d%d%d",&n,&m,&t))
{
memset(dp,-1,sizeof(dp));
for(int i=1;i<=m;i++)
{
scanf("%d%d",&pos[i],&val[i]);
}
Dfs(0,1,t);
printf("Scenario #%d\n",++kase);
printf("%d\n\n",dp[0][1][t]);
}
}