1.解题思路
主要分三个操作:召唤随从,发起攻击,结束,其中,召唤随从分为该位置没有随从和有随从这两种情况,没有随从则直接赋值,有随从需要将该位置右边的随从全部向右移一位;发起攻击首先计算出攻击后双方的生命值,判断是否小于等于0,若攻击方或被攻击方编号是0(即相当于英雄死亡),判断获胜方后直接退出,否则将该位置右边的随从全部向左移一位(相当于随从死亡);结束只需要用1-order进行切换。
2.满分代码
#include<iostream>
using namespace std;
int n;
string str;
struct node{
int health;//生命值
int attack;//攻击值
};
struct player{
node people[8];//0表示英雄
}p[2];//先手vs后手
int main()
{
cin>>n;
int order=0;
int flag=0;
p[0].people[0].health=30;
p[0].people[0].attack=0;
p[1].people[0].health=30;
p[1].people[0].attack=0;
while(n--)
{
cin>>str;
if(str=="summon")//召唤随从
{
int position,attack,health;
cin>>position>>attack>>health;
if(p[order].people[position].attack==0&&p[order].people[position].health==0)//该位置没有随从
{
p[order].people[position].health=health;
p[order].people[position].attack=attack;
}
else//若该位置有随从 则其右边的随从都向右移一位
{
for(int i=6;i>=position;i--)
{
p[order].people[i+1].health=p[order].people[i].health;
p[order].people[i+1].attack=p[order].people[i].attack;
}
p[order].people[position].attack=attack;
p[order].people[position].health=health;
}
}
else if(str=="attack")//攻击
{
int attacker,defender;
cin>>attacker>>defender;
p[order].people[attacker].health-=p[1-order].people[defender].attack;
p[1-order].people[defender].health-=p[order].people[attacker].attack;
if(p[order].people[attacker].health<=0)//当前方有人死亡
{
if(attacker==0)// 英雄死亡
{
if(order==0)flag=-1;
else flag=1;
break;
}
for(int i=attacker+1;i<=7;i++)//随从死亡
{
p[order].people[i-1].health=p[order].people[i].health;
p[order].people[i-1].attack=p[order].people[i].attack;
}
p[order].people[7].attack=0;
p[order].people[7].health=0;
}
if(p[1-order].people[defender].health<=0)//对方有人死亡
{
if(defender==0)
{
if(order==0)flag=1;
else flag=-1;
break;
}
for(int i=defender+1;i<=7;i++)
{
p[1-order].people[i-1].health=p[1-order].people[i].health;
p[1-order].people[i-1].attack=p[1-order].people[i].attack;
}
p[1-order].people[7].attack=0;
p[1-order].people[7].health=0;
}
}
else if(str=="end")
{
order=1-order;
}
}
cout<<flag<<endl;
cout<<p[0].people[0].health<<endl;
int cnt=0;
for(int i=1;i<=7;i++)
{
if(p[0].people[i].attack&&p[0].people[i].health)
cnt++;
}
cout<<cnt<<" ";
for(int i=1;i<=7;i++)
{
if(p[0].people[i].attack&&p[0].people[i].health)
cout<<p[0].people[i].health<<" ";
}
cout<<endl;
cout<<p[1].people[0].health<<endl;
cnt=0;
for(int i=1;i<=7;i++)
{
if(p[1].people[i].attack&&p[1].people[i].health)
cnt++;
}
cout<<cnt<<" ";
for(int i=1;i<=7;i++)
{
if(p[1].people[i].attack&&p[1].people[i].health)
cout<<p[1].people[i].health<<" ";
}
return 0;
}