问题描述
Sample Input
8
summon 1 3 6
summon 2 4 2
end
summon 1 4 5
summon 1 2 1
attack 1 2
end
attack 1 1
Sample Output
0
30
1 2
30
1 2
解题思路以及关键代码
因为有很多插入删除操作,此题采用vector很方便。
要注意一个坑 !!!不要把英雄像删随从一样删掉了!英雄和随从死亡的判定是生命值h<=0。
如果英雄死亡,不会有后续操作,所以不用特判来break。
全部代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
struct node
{
int hx,ax;
};
vector<node> fo1,fo2;//0-7
bool flag=true;
void func(const string &s)
{
if(s=="summon")
{
int pos,at,he;
cin>>pos>>at>>he;
if(flag)
fo1.insert(fo1.begin()+pos,{he,at});
else
fo2.insert(fo2.begin()+pos,{he,at});
}
else if(s=="attack")
{
int ater,defer;
cin>>ater>>defter;
if(!flag) swap(ater,defer);
fo1[ater].hx-=fo2[defer].ax;
fo2[defer].hx-=fo1[ater].ax;
if(fo1[ater].hx<=0 && ater!=0)
fo1.erase(fo1.begin()+ater);
if(fo2[defer].hx<=0 && defer!=0)
fo2.erase(fo2.begin()+defer);
}
else if(s=="end") flag=!flag;
}
int main()
{
int n;
cin>>n;
fo1.insert(fo1.begin(),{30,0});
fo2.insert(fo2.begin(),{30,0});
for (int i=1; i<=n; i++)
{
string s; cin>>s;
func(s);
}
if(fo1[0].hx<=0)
printf("-1\n");
else if(fo2[0].hx<=0)
printf("1\n");
else
printf("0\n");
printf("%d\n%lu",fo1[0].hx,fo1.size()-1);
for (int i=1; i<fo1.size(); i++)
printf(" %d",fo1[i].hx); printf("\n");
printf("%d\n%lu",fo2[0].hx,fo2.size()-1);
for (int i=1; i<fo2.size(); i++)
printf(" %d",fo2[i].hx); printf("\n");
return 0;
}