Description
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.
Input
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).
Output
If LvBu is dead output “Poor LvBu,his period was gone.”
Or output the maximum HP left.
Sample Input
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
Sample Output
30
Poor LvBu,his period was gone.
核心思想:
状压dp。
最多有20个敌人,用0代表还未和吕布交战,1代表已经和吕布交战。20位的二进制数可以表示出所有交战与否的状态。
对于任何一个状态 i,谁和吕布交战过是确定的,因此吕布此状态下经验值exp是唯一确定的,等级也就唯一确定,又因为吕布初始的攻击力和防御力是给定的,所以状态 i 下吕布的攻击力ati和防御力def也是唯一确定的。
只有生命值hp和交战顺序有关,不是唯一确定的,需要dp。
dp[i]代表状态 i 下的生命值hp。
从 i 状态转移到 ne 状态,hp应减去交战损失的re并加上升级得到的
int te=dp[i]-re+((exp+enemy[e].exp)/100-exp/100)*INHP;
dp[ne]=max(dp[ne],te);
详见代码注释。
代码如下:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=21;
struct node{
int ati,def,hp,exp;
}enemy[N];//敌人属性
int dp[1<<N];//dp值即生命值hp
int n,ATI,DEF,HP,INATI,INDEF,INHP;//吕布初始属性
char s[40];
//前三者为吕布现在的属性,e为敌人编号
//吕布胜则返回吕布此战损失的hp,败则返回-1
int fight(int ati,int def,int hp,int e)
{
int lvhp=0,ehp=0;
while(1)
{
ehp+=max(1,ati-enemy[e].def);
if(ehp>=enemy[e].hp)
return lvhp;
lvhp+=max(1,enemy[e].ati-def);
if(lvhp>=hp)
return -1;
}
return -1;
}
int main()
{
while(scanf("%d%d%d%d%d%d",&ATI,&DEF,&HP,&INATI,&INDEF,&INHP)!=EOF)
{
scanf("%d",&n);
//初始化
int end=1<<n;
for(int i=0;i<end;i++)
dp[i]=0;
dp[0]=HP;
//读入
for(int i=0;i<n;i++)
scanf("%s%d%d%d%d",s,&enemy[i].ati,&enemy[i].def,&enemy[i].hp,&enemy[i].exp);
//状压dp
for(int i=0;i<end;i++)
{
//如果吕布在i状态时生命值为0,他无法在此状态发动攻击
//没有此判断会WA
if(!dp[i])continue;
//对于确定的状态i,吕布的ati,def,exp都是确定的
int exp=0;
for(int k=1,e=0;k<end;k<<=1,e++)
if(i&k)
exp+=enemy[e].exp;
int ati=ATI+exp/100*INATI;
int def=DEF+exp/100*INDEF;
//dp,吕布在剩余的敌人中(遍历)任选一个攻击
//e为敌人编号,k为e号敌人对应的二进制权值
for(int k=1,e=0;k<end;k<<=1,e++)
{
//吕布与e号敌人交战过则continue
if(i&k)continue;
//吕布与e号敌人交战完成后状态变为i|k
int ne=i|k,re=fight(ati,def,dp[i],e);
//re不是-1,则吕布胜,更新dp[ne]
if(re>=0)
{
//从i状态转移到ne状态,hp应减去交战损失的并加上升级得到的
int te=dp[i]-re+((exp+enemy[e].exp)/100-exp/100)*INHP;
dp[ne]=max(dp[ne],te);
}
}
}
//输出
if(dp[end-1])
printf("%d\n",dp[end-1]);
else
printf("Poor LvBu,his period was gone.\n");
}
return 0;
}
来源:https://blog.csdn.net/Nothing_but_Fight/article/details/99672074