状态压缩。不堪回首的一题,很水,但是调了一下午。重写了三遍。细节方面需要注意点。题目给的条件多了,脑子就乱了,其实大部分条件都只是干扰项。还是要先分析清楚再写,不然脑子混混沌沌的很容易出错,效率太低。
1.一开始开了个二维,一维是状态,一维是灭了几个人了。后来发现不用记录灭了多少人,每个人只能被灭一次,状态直接可以显示出人数。
2.当攻击低于防御时会强制扣一点血
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
#define INF 1000000000
#define N 1000
int n;
int def, ati, hp, inati, indef, inhp;
int dp[1 << 20][4];
struct En{
int def;
int ati;
int hp;
int exp;
}e[30], mid;
int main(){
while(scanf("%d%d%d%d%d%d", &ati, &def, &hp, &inati, &indef, &inhp) != EOF){
scanf("%d", &n);
char ch[30];
for(int i = 0; i < n; i ++){
scanf("%s%d%d%d%d", ch, &e[i].ati, &e[i].def, &e[i].hp, &e[i].exp);
}
memset(dp, 0, sizeof(dp));
dp[0][0] = hp;
dp[0][1] = ati;
dp[0][2] = def;
dp[0][3] = 0;
for(int i = 0; i < (1 << n); i++){
for(int j = 0; j < n; j++){
if(i & (1 << j))continue;
mid.ati = dp[i][1];
mid.def = dp[i][2];
mid.hp = dp[i][0];
mid.exp = dp[i][3];
int hurt0 = max(1, mid.ati - e[j].def);
int hurt1 = max(1, e[j].ati - mid.def);
int t = (e[j].hp - 1) / hurt0;
if(t * hurt1 >= mid.hp)continue;
mid.hp -= t * hurt1;
mid.exp += e[j].exp;
if(mid.exp >= 100){
mid.exp -= 100;
mid.hp += inhp;
mid.ati += inati;
mid.def += indef;
}
if(mid.hp >= dp[i | (1 << j)][0]){
dp[i | (1 << j)][0] = mid.hp;
dp[i | (1 << j)][1] = mid.ati;
dp[i | (1 << j)][2] = mid.def;
dp[i | (1 << j)][3] = mid.exp;
}
}
}
if(dp[(1 << n) - 1][0] > 0) printf("%d\n", dp[(1 << n) - 1][0]);
else printf("Poor LvBu,his period was gone.\n");
}
return 0;
}