心得:
写这种题关键就是操作分开写,写完一个测试一个。要不然代码多,调试困难,思路乱,导致做错。
代码:
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct sss{
int health;
int attack;
};
int f,fff;
struct hero{
int cnt;
struct sss sc[10];
}h[2];
int is_c(string t)
{
if(t == "summon")
return 1;
else if(t == "attack")
return 2;
else if(t == "end")
return 3;
else return 4;
}
void _init()
{
h[0].cnt = h[1].cnt = 0;
memset(h[0].sc,0,sizeof h[0].sc);
memset(h[1].sc,0,sizeof h[1].sc);
h[0].sc[0].health = h[1].sc[0].health = 30;
}
void _summon(int pos,int attack,int health)
{
h[f % 2].cnt++;
for(int i = h[f % 2].cnt;i > pos;--i)
{
h[f % 2].sc[i] = h[f % 2].sc[i - 1];
}
h[f % 2].sc[pos].attack = attack;
h[f % 2].sc[pos].health = health;
}
void _delete(int pos,int ff)
{
int a = (f + ff) % 2;
for(int i = pos;i <= h[a].cnt;++i)
h[a].sc[i] = h[a].sc[i + 1];
h[a].cnt--;
}
void _attack(int pos_a,int pos_d)
{
int a = f % 2;
int b = (f + 1) % 2;
h[a].sc[pos_a].health -= h[b].sc[pos_d].attack;
h[b].sc[pos_d].health -= h[a].sc[pos_a].attack;
if(h[a].sc[pos_a].health <= 0)
_delete(pos_a,0);
if(h[b].sc[pos_d].health <= 0 && pos_d)
_delete(pos_d,1);
if(h[b].sc[pos_d].health <= 0 && !pos_d)
fff = 1;
}
void _print()
{
if(h[0].sc[0].health > 0 && h[1].sc[0].health > 0)
printf("0\n");
else if(h[0].sc[0].health > 0 && h[1].sc[0].health <= 0)
printf("1\n");
else printf("-1\n");
printf("%d\n%d",h[0].sc[0].health,h[0].cnt);
for(int i = 1;i <= h[0].cnt;++i)
printf(" %d",h[0].sc[i].health);
printf("\n%d\n%d",h[1].sc[0].health,h[1].cnt);
for(int i = 1;i <= h[1].cnt;++i)
printf(" %d",h[1].sc[i].health);
printf("\n");
}
int main()
{
int n;
int pos,attack,health;
int pos_a,pos_d;
string t;
f = fff = 0;
while(~scanf("%d",&n))
{
_init();
while(n--)
{
cin >> t;
if(is_c(t) == 1) //summon
{
cin >> pos >> attack >> health;
if(!fff)
_summon(pos,attack,health);
}
else if(is_c(t) == 2) //attack
{
cin >> pos_a >> pos_d;
if(!fff)
_attack(pos_a,pos_d);
}
else if(is_c(t) == 3) // end
{
f++;
}
}
_print();
}
return 0;
}