首页 > ACM题库 > HDU-杭电 > HDU 2809-God of War-动态规划-[解题报告]HOJ
2014
02-17

HDU 2809-God of War-动态规划-[解题报告]HOJ

God of War

问题描述 :

At 184~280 A.D ,there were many kingdoms in China. Three strongest among them are "Wei", "Shu", "Wu". People call this period as "Three Kingdoms".
HH is a super "Three Kingdoms" fan, because at this period there were many heroes and exciting stories. Among the heroes HH worships LvBu most.
LvBu is the God of War and is also intelligent, but his ambition is too big while enemies are too powerful .Many monarchs wanted to kill him.
At 198 A.D ,CaoCao fought with LvBu at Xuzhou.Though Lvbu is the God of War ,CaoCao had so many generals: Xuchu,DianWei XiahouChun……Facing so many heroes ,could LvBu beat all of them?

Given the LvBu’s ATI, DEF, HP, and enemies’ ATI, DEF,HP, experience (if LvBu killed one of his enemies, he can get that experience ,and if his experience got more than or equal to 100*level,he would level-up and become stronger) and the In_ATI,In_DEF,In_HP(indicating when LvBu levels up,his ability will increase this point).
Each turn LvBu will choose an enemy to fight. Please help LvBu find a way to beat all of enemies and survive with the max HP.
Here’s a fight between LvBu and A:
If LvBu attack A, A will lose Max(1,LvBu’s ATI- A’s DEF) hp;
If A survived, he will give LvBu Max(1,A’ATI- LvBu’DEF) injury.
If LvBu is still alive, repeat it untill someone is dead(hp <= 0).

LvBu’s initial level is 1 and experience is 0,and he can level up many times.

输入:

The input contains at most 20 test cases.
For each case , the first line contains six intergers ,indicating LvBu’s ATI,DEF,HP and In_ATI,In_DEF,In_HP.
The next line gives an interger N(0<N<=20),indicating the number of the enemies .
Then N lines followed, every line contains the name(the length of each name is no more than 20),ATI,DEF,HP, experience(1<experience<=100).

输出:

The input contains at most 20 test cases.
For each case , the first line contains six intergers ,indicating LvBu’s ATI,DEF,HP and In_ATI,In_DEF,In_HP.
The next line gives an interger N(0<N<=20),indicating the number of the enemies .
Then N lines followed, every line contains the name(the length of each name is no more than 20),ATI,DEF,HP, experience(1<experience<=100).

样例输入:

100  80  100  5  5  5
2
ZhangFei 95  75  100  100 
XuChu 90  90  100  90

100 75 100 5 5 5
1
GuanYu 95 85 100 100

样例输出:

30
Poor LvBu,his period was gone.

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2809



题目大意:奥特曼很牛逼,要单挑n只怪兽。怪兽和奥特曼一样都有hp、攻击力、防御力,奥特曼有一个经验值属性,通过打怪兽获得经验值超过100就升级,升级时hp加一些,攻击力加一些,防御力加一些,回不到满血状态,奥特曼每次都要和怪兽血拼,奥特曼先打,怪兽后打,直到一方倒下为止。问奥特曼能不能打倒所有的怪兽从而拯救地球?可以的话输出剩下的最大hp。



解题思路:题目看起来很雷人,实际上就是普通的TSP,只是每次状态转移略显麻烦些。为什么可以转换为TSP呢?因为每只怪兽都只要打一次,而且打完若干只怪兽后剩下的经验值、攻击力、防御力都是一样的,只有hp会随顺序改变,那么用一个数组dp[i]表示i状态时的最多hp,每次更新都去计算i状态下的各项属性。我把hp,at,def,ex都封装进一个结构体,意思是一样的,状态转移见代码。


测试数据:

100  80  100  5  5  5
2
ZhangFei 95  75  100  100 
XuChu 90  90  100  90

100 75 100 5 5 5
1
GuanYu 95 85 100 100


C艹代码:

#include <stdio.h>
#include <string.h>
#define MIN 21
#define MAX 1<<21
#define max(a,b) (a)>(b)?(a):(b)


struct node {

    int at,def,hp,ex;
}dp[MAX],enemy[MIN],add;
int n,m,state,ans;


void State_Dp() {

    int i,j,k,meat,heat,mtime,htime;
    int curhp,curex,curat,curdef;


    for (i = 0; i < (1<<n); ++i)
        for (j = 0; j < n; ++j)
            if (!(i & (1<<j)) && dp[i].hp) {

                meat = max(1,dp[i].at - enemy[j].def);          //我们打对方多少hp
                heat = max(1,enemy[j].at - dp[i].def);          //对方打我们多少hp
                mtime = (dp[i].hp + heat - 1) / heat;           //我们能撑住几次攻击
                htime = (enemy[j].hp + meat - 1) / meat;        //对方能撑住几次攻击


                if (mtime < htime) continue;                  //我们先倒下
                curhp = dp[i].hp - (htime - 1) * heat;        //攻击完的状态
                curex = dp[i].ex + enemy[j].ex;
                curat = dp[i].at,curdef = dp[i].def;
                if (curex >= 100) {                           //升级

                    curex -= 100;
                    curhp += add.hp;
                    curat += add.at;
                    curdef += add.def;
                }
                

                if (curhp >= dp[i|(1<<j)].hp) {             //更新答案

                    dp[i|(1<<j)].hp = curhp;
                    dp[i|(1<<j)].ex = curex;
                    dp[i|(1<<j)].at = curat;
                    dp[i|(1<<j)].def = curdef;
                }
            }
}


int main()
{
    int i,j,k;
    char tp[30];


    while (scanf("%d%d%d",&dp[0].at,&dp[0].def,&dp[0].hp) != EOF) {

        dp[0].ex = 0;
        scanf("%d%d%d",&add.at,&add.def,&add.hp);
        scanf("%d",&n);
        for (i = 0; i < n; ++i)
            scanf("%s %d%d%d%d",tp,&enemy[i].at,&enemy[i].def,&enemy[i].hp,&enemy[i].ex);

        
        for (i = 1; i < (1<<n); ++i) dp[i].hp = 0;
        State_Dp();
        if (dp[(1<<n)-1].hp) printf("%d\n",dp[(1<<n)-1].hp);
        else printf("Poor LvBu,his period was gone.\n");
    }
}

本文ZeroClock原创,但可以转载,因为我们是兄弟。

解题参考:http://blog.csdn.net/woshi250hua/article/details/7746780


  1. #include <cstdio>

    int main() {
    //answer must be odd
    int n, u, d;
    while(scanf("%d%d%d",&n,&u,&d)==3 && n>0) {
    if(n<=u) { puts("1"); continue; }
    n-=u; u-=d; n+=u-1; n/=u;
    n<<=1, ++n;
    printf("%dn",n);
    }
    return 0;
    }

  2. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥