思路
模拟数据操作:
首先选用数据结构,存储的信息有英雄的攻击力和生命值,7个随从的编号,生命值,攻击力。可以用pair<int,int>
数组,但是随着游戏进行,随从会增加和死亡,会移动随从,改变随从编号,所以用pair<int,pair<int,int>>
,来存储信息,分别表示随从的编号,生命值,攻击力。
随后模拟上随从,攻击,交换玩家操作即可。
具体看代码。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
const int inf = 0x3f3f3f;
using namespace std;
int n, x, y, z, acc, bcc, a_alive, b_alive;
bool flag,f;
pair<int, pair<int, int> > a[8]; //先手玩家
pair<int, pair<int, int> > b[8]; //后手玩家
pair<int, int> ac; //参与攻击的 先手玩家的随从信息
pair<int, int> bc; 参与攻击的 后手玩家的随从信息
int main()
{
flag = true; //初始为true 代表先手玩家
a_alive = b_alive = 0; //先手玩家后手玩家存活随从数
for (int i = 0; i < 8;i++)
{//初始化
a[i].first = b[i].first = inf; //所有人编号为inf
a[i].second.first = a[i].second.second = b[i].second.first = b[i].second.second = 0; //生命值攻击力全为0
}
a[0] = make_pair(0, make_pair(0, 30)); //两个英雄 只有30生命值
b[0] = make_pair(0, make_pair(0, 30)); //且序号为0
string s;
scanf("%d", &n);
while (n--)
{
cin >> s;getchar(); //读操作
if (s == "summon")
{
f = true; //是否执行此次操作 true为没有 将要执行
scanf("%d %d %d", &x, &y, &z);
if (flag)
{//先手玩家
for (int i = 1; i < 8;i++)
{//从1开始 排除英雄 只算随从
if(a[i].first >= x && a[i].first != inf)
{ //序号大于x的 都+1
a[i].first++;
}
else if(f && a[i].first == inf )
{ //没有操作过且 需要为inf的 上随从
a[i].first = x;
a[i].second.first = y;
a[i].second.second = z;
f = false; //执行过了 也可以去掉f 改用break
}
}
a_alive++; //增加 随从数++
}
else
{//后手玩家 与上面一样
for (int i = 1; i < 8;i++)
{
if(b[i].first >= x && b[i].first != inf)
{
b[i].first++;
}
else if(f && b[i].first == inf )
{
b[i].first = x;
b[i].second.first = y;
b[i].second.second = z;
f = false;
}
}
b_alive++;
}
}
else if (s == "attack")
{//攻击
acc = 0; //记录 先手玩家参与攻击的随从 所在数组的位置
bcc = 0; //记录 后手玩家参与攻击的随从 所在数组的位置
scanf("%d %d", &x, &y);
if (flag)
{//先手攻击
for (int i = 0; i < 8; i++)
{
if(a[i].first==x)
{ //记录参与攻击的先手玩家
acc = i;
ac.first = a[i].second.first;
ac.second = a[i].second.second;
}
if(b[i].first==y)
{//记录参与攻击的后手玩家
bcc = i;
bc.first = b[i].second.first;
bc.second = b[i].second.second;
}
}
ac.second = ac.second - bc.first; //攻击
bc.second = bc.second - ac.first;
if(ac.second <= 0 )
{ //判断随从是否死亡
a_alive--; //随从-1
for(int i=1;i<8;i++)
{
if(a[i].first > a[acc].first)
{
a[i].first--; //所有大于此随从的序号-1
}
}
a[acc].first=inf; //将此随从需要为inf 设为默认
}
if(bc.second <=0 && b[bcc].first != 0 )
{ //后手玩家 与上面一样
b_alive--;
for(int i=1;i<8;i++)
{
if(b[i].first > b[bcc] .first)
{
b[i].first--;
}
}
b[bcc].first=inf;
}
a[acc].second = ac; //赋值攻击后的信息
b[bcc].second = bc;
}
else
{//后手攻击 与上面类似
for (int i = 0; i < 8; i++)
{
if(a[i].first==y)
{
acc = i;
ac.first = a[i].second.first;
ac.second = a[i].second.second;
}
if(b[i].first==x)
{
bcc = i;
bc.first = b[i].second.first;
bc.second = b[i].second.second;
}
}
ac.second = ac.second - bc.first;
bc.second = bc.second - ac.first;
if(ac.second <= 0 && a[acc].first != 0 )
{
a_alive--;
for(int i=1;i<8;i++)
{
if(a[i].first > a[acc].first)
{
a[i].first--;
}
}
a[acc].first=inf;
}
if(bc.second <=0 )
{
b_alive--;
for(int i=1;i<8;i++)
{
if(b[i].first > b[bcc] .first)
{
b[i].first--;
}
}
b[bcc].first=inf;
}
a[acc].second = ac;
b[bcc].second = bc;
}
}
else if (s == "end")
{//换人
if (flag)
{
flag = false;
}
else
{
flag = true;
}
}
}
sort(a + 1, a + 7 + 1); //按随从序号排序
sort(b + 1, b + 7 + 1);
//输出
if(a[0].second.second > 0 && b[0].second.second <= 0)
{
printf("1\n");
}
else if(a[0].second.second <= 0 && b[0].second.second > 0 )
{
printf("-1\n");
}
else
{
printf("0\n");
}
printf("%d\n", a[0].second.second);
printf("%d", a_alive);
for (int i = 1; i < 8;i++)
{
if(a[i].second.second > 0 && a[i].second.second < inf )
{
printf(" %d", a[i].second.second);
}
}
printf("\n");
printf("%d\n", b[0].second.second);
printf("%d", b_alive);
for (int i = 1; i < 8;i++)
{
if(b[i].second.second > 0 && b[i].second.second < inf )
{
printf(" %d", b[i].second.second);
}
}
return 0;
}
总结:
注意次序,注意格式!!