The "endless" model in "I, zombie" of "Plants vs. Zombies" is my favourite. The aim of the game is to put down the zombies most reasonable in the right-most , to eat the brain protected by Plants in the left-most.To simplify the problem ,we provide only one kind of Zombies : normal Zombies with 50 hp each. The map contains one row only, and we provide two kinds of plants : Peashooter (with 10hp) and Potato Mine. The operating steps of every second are as below(Warning: make sure your program strictly follow the order of these steps):
1.Put some or none zombies in the right-most grid one by one. (In the right grid of the right-most plant at beginning). 2.Judge every survived zombie, whether he's standing on a grid with a Peashooter. 2.1.If it's true, he attacks the Peashooter in his grid, and the Peashooter decreases 1 hp. The Peashooter's hp may be negative at that moment, but it's still alive! 2.2.If it's false, he moves left for one grid. 3.If there are still some zombies in the map, every survived Peashooter will shoot a pea to the zombie who was put earliest. (the zombie's hp decreases 1 hp for each pea, the zombie's hp can be negative at that moment, but it's still alive!) 4.If there are zombies in the grids where Potato Mine stands , then the Potato Mine explodes , all the zombies' hp in this grid become 0. 5.The plants and zombies with non-positive hp disappear(until now they are dead).
Now, given the map, you are to tell me how many zombies are needed at least, in order to eat the brain in the left-most?
1.Put some or none zombies in the right-most grid one by one. (In the right grid of the right-most plant at beginning). 2.Judge every survived zombie, whether he's standing on a grid with a Peashooter. 2.1.If it's true, he attacks the Peashooter in his grid, and the Peashooter decreases 1 hp. The Peashooter's hp may be negative at that moment, but it's still alive! 2.2.If it's false, he moves left for one grid. 3.If there are still some zombies in the map, every survived Peashooter will shoot a pea to the zombie who was put earliest. (the zombie's hp decreases 1 hp for each pea, the zombie's hp can be negative at that moment, but it's still alive!) 4.If there are zombies in the grids where Potato Mine stands , then the Potato Mine explodes , all the zombies' hp in this grid become 0. 5.The plants and zombies with non-positive hp disappear(until now they are dead).
Now, given the map, you are to tell me how many zombies are needed at least, in order to eat the brain in the left-most?
Input
The first line is a number T (1<=T<=100), represents the number of cases. The next T line with n(0<n<=100) characters ,represents each case ,'P' represents Peashooters ,'M' represents Potato Mine ,no other character in the input.
Output
Output a number represents the minimum zombies needed to eat the brain in the left-most. Following the case number (start with 1).
Sample Input
5
PM
PP
PPP
MMMMM
PPMM
Sample Output
Case 1: 2
Case 2: 1
Case 3: 2
Case 4: 6
Case 5: 3
题意:模拟植物大战僵尸,地图只有一行,有两种植物:豌豆射手(P)和炸弹土豆(M),僵尸要进攻,每只僵尸的hp是50,每个豌豆射手的hp是10,
僵尸如果走在土豆上,土豆会爆炸,所有在土豆处的僵尸hp都变为0,如果僵尸走在豌豆射手处,则每只僵尸对豌豆射手攻击一次,造成1点的伤害,
豌豆射手会攻击走在最前面的僵尸,每个豌豆射手攻击一次,造成1点的伤害。问最少需要多少只僵尸才能赢。
解析:很恶心的模拟,要注意的是僵尸不是一个接着一个排成队放在右边,可以几个僵尸放在同一个位置开始进攻,不然如果一开始就有超过50个
豌豆射手,那岂不是进来一个死一个(我最开始以为是一个一个放,以为不会有这种无解的情况。。。。。。),如果僵尸碰到的是炸弹,那么下一
回合可以认为僵尸的最左位置为炸弹的左边。如果碰到的不是炸弹,则不停向左统计植物个数,直到遇到第一个炸弹,然后二分可以吃掉这些植物
的最少僵尸个数。详见代码实现。
代码
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iterator>
#include<stack>
using namespace std;
const int maxn=;
char S[maxn];
int L,Pnum,Mnum,step;
void init()
{
L=strlen(S)-;
Pnum=Mnum=; //豌豆射手与土豆的个数
for(int i=;i<=L;i++)
if(S[i]=='P') Pnum++;
else Mnum++;
}
bool judge(int totpnum,int pnum,int znum)
{
int nowstep=step;
int P_hp=,Z_hp=; //豌豆射手和僵尸的hp
while(pnum>&&znum>)
{
if(nowstep>){ nowstep--; Z_hp-=totpnum; } //没走到豌豆射手处
else{ P_hp-=znum; Z_hp-=totpnum; } //走到了两个都要受伤害
if(P_hp<=) //死一个射手
{
P_hp=;
totpnum--; pnum--;
nowstep=;
}
if(Z_hp<=){ Z_hp=; znum--; } //死一个僵尸
}
if(pnum<=&&znum>) return true; //所有豌豆射手挂掉而僵尸有多的
return false;
}
int BIS(int totpnum,int pnum) //二分
{
int x=,y=*maxn,mid;
while(x<=y)
{
mid=(x+y)/;
if(judge(totpnum,pnum,mid)) y=mid-;
else x=mid+;
}
return y+;
}
int solve()
{
int ret=; //ret是需要的僵尸的个数
if(S[L]=='M') { ret++; L--; step=; } //最右边是土豆,step代表走到下一个位置需要的步数
else step=;
while(L>=) //知道小于0才算赢
{
if(S[L]=='M') //是土豆
{
if(step>) //往前走
{
step--;
if(Pnum>=) ret++; //超过50个豌豆射手死一个僵尸
continue;
}
else ret++; //炸死一个
Mnum--; L--;
step=; //step变为2
}
else //如果是豌豆射手
{
int pnum=;
for(int i=L;i>=;i--) //一直到找到土豆
if(S[i]=='M') break;
else pnum++;
ret+=BIS(Pnum,pnum); //二分得到最小的僵尸个数
Pnum-=pnum;
L-=pnum+; //多减1表示把土豆算进去了,因为有没死的僵尸在土豆处牺牲
step=;
}
}
if(S[]=='M') ret++; //如果最左边是土豆,则还需要一个僵尸去攻击首脑
return ret;
}
int main()
{
int T,Case=;
scanf("%d",&T);
while(T--)
{
scanf("%s",S); //植物
init();
printf("Case %d: %d\n",++Case,solve());
}
return ;
}