hdu 3008:Warcraft(动态规划 背包)

Warcraft

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 957    Accepted Submission(s): 487


Problem Description
Have you ever played the Warcraft?It doesn't matter whether you have played it !We will give you such an experience.There are so many Heroes in it,but you could only choose one of them.Each Hero has his own skills.When such a Skill is used ,it costs some MagicValue,but hurts the Boss at the same time.Using the skills needs intellegence,one should hurt the enemy to the most when using certain MagicValue.

Now we send you to complete such a duty to kill the Boss(So cool~~).To simplify the problem:you can assume the LifeValue of the monster is 100, your LifeValue is 100,but you have also a 100 MagicValue!You can choose to use the ordinary Attack(which doesn't cost MagicValue),or a certain skill(in condition that you own this skill and the MagicValue you have at that time is no less than the skill costs),there is no free lunch so that you should pay certain MagicValue after you use one skill!But we are good enough to offer you a "ResumingCirclet"(with which you can resume the MagicValue each seconds),But you can't own more than 100 MagicValue and resuming MagicValue is always after you attack.The Boss is cruel , be careful!
 
Input
There are several test cases,intergers n ,t and q (0<n<=100,1<=t<=5,q>0) in the first line which mean you own n kinds of skills ,and the "ResumingCirclet" helps you resume t points of MagicValue per second and q is of course the hurt points of LifeValue the Boss attack you each time(we assume when fighting in a second the attack you show is before the Boss).Then n lines follow,each has 2 intergers ai and bi(0<ai,bi<=100).which means using i skill costs you ai MagicValue and costs the Boss bi LifeValue.The last case is n=t=q=0.
 
Output
Output an interger min (the minimun time you need to kill the Boss)in one line .But if you die(the LifeValue is no more than 0) ,output "My god"!
 
Sample Input
4 2 25 10 5 20 10 30 28 76 70 4 2 25 10 5 20 10 30 28 77 70 0 0 0
 
Sample Output
4 My god
Hint
Hint: When fighting,you can only choose one kind of skill or just to use the ordinary attack in the whole second,the ordinary attack costs the Boss 1 points of LifeValue,the Boss can only use ordinary attack which costs a whole second at a time.Good Luck To You!
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  1158 1227 1300 1501 1502 
 
  动态规划的背包问题。
  不明白什么是背包问题见链接:百度百科 - 背包问题
  一开始没想到怎么做,看了解题报告才有了思路。链接在这:HDU 3008 背包
  
  dp[i][j]代表第 i 秒剩余 j 魔法值时所作出的伤害。
  思路:循环时间 i ,不断产生第 i 秒的所有子问题(dp[i][0..100]),记录可能的情况的伤害值,其他情况记录为-1。累积所有子问题直到出现符合情况(dp[i][j]>=100)的子问题,退出循环输出结果(一定是最min的)。或是达到限定时间(因为到达这个时间的时候,你已经被BOSS打的没血了)。
  注意剪枝以及一些边界情况,细节没处理好很容易WA。
  
  代码可能不是很简练,但是我觉得还是容易看懂的。
  
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 int a[105],b[105];
 6 int dp[105][105];    //代表到第 i 秒剩余 j 魔法值打掉BOSS的HP 
 7 int main()
 8 {
 9     int n,t,q;
10     while(cin>>n>>t>>q){
11         if(n==0 && t==0 && q==0)
12             break;
13         for(int i=1;i<=n;i++)
14             cin>>a[i]>>b[i];
15         //freopen("output.txt","w",stdout);
16         memset(dp,-1,sizeof(dp));
17         dp[0][100]=0;    //刚开始的时候出了MP=100,其余都为无效
18         int T;
19         if(100%q==0)
20             T=100/q;
21         else
22             T=100/q+1;
23         int i,j,k;
24         for(i=1;i<=T;i++){    //最多到100秒
25             for(j=0;j<=100;j++){    //一次遍历dp[i-1][0..100]的所有情况 
26                 if(dp[i-1][j]==-1)
27                     continue;
28                 for(k=1;k<=n;k++){    //使用技能攻击 
29                     if(j-a[k]<0)     //如果剩余的MP不足以使用这个技能,则跳出本次循环 
30                         continue;
31                     int temp;
32                     temp=j-a[k]+t>100?100:j-a[k]+t;
33                     if(dp[i][temp]==-1){    //没到达过这个MP 
34                         dp[i][temp]=dp[i-1][j]+b[k]; 
35                         if(dp[i][temp]>=100)
36                             goto label;
37                     }
38                     else{    //说明之前有降到过这个MP,这个时候比较哪一个伤害高,记录伤害高的值 
39                         if(dp[i-1][j]+b[k] > dp[i][temp]){    //如果当前技能伤害高
40                             dp[i][temp]=dp[i-1][j]+b[k]; 
41                             if(dp[i][temp]>=100)
42                                 goto label;
43                         }
44                     }
45                 }
46                 if(j+t>=100){    //用普通攻击
47                     if(dp[i-1][j]+1 > dp[i][100]) {
48                         dp[i][100] = dp[i-1][j]+1;
49                         if(dp[i][100]>=100)
50                             goto label;
51                     }
52                 }
53                 else{
54                     if(dp[i-1][j]+1 > dp[i][j+t]){
55                         dp[i][j+t]=dp[i-1][j]+1;
56                         if(dp[i][j+t]>=100)
57                             goto label;
58                     }
59                 }
60             }
61         }
62         label:
63         if(i>T)    //正常退出,代表在规定时间内没打死BOSS,你挂了 
64             cout<<"My god"<<endl;
65         else
66             cout<<i<<endl;
67     }
68     return 0;
69 }

Freecode : www.cnblogs.com/yym2013

原文地址:https://www.cnblogs.com/yym2013/p/3533914.html