炉石传说
原题链接
注意点:
1.战场的随从都是从1开始编号的
2.麻烦点在于随从的插入和删除需要进行大量的移动操作->.n的范围较小,可以直接来进行移动
3.当本方战场有 7 个随从时,不会再召唤新的随从->不会出现溢出的情况
#include<iostream>
using namespace std;
#define NMAX 8
int n;
int position, attack, health, attacker, defender;
int order = 0;
struct person {
int attack, health;
}war[2][NMAX];
int anum[2];
void insert(int o) {
for (int i = anum[o]; i >= position; i--) {
swap(war[o][i], war[o][i + 1]);
}
war[o][position].attack = attack;
war[o][position].health = health;
anum[o]++;
}
void clear(int o, int p) {
if (!p) return; //如果是hero死亡了,不要移动
for (int i = p + 1; i <= anum[o]; i++) { //如果p=1,
swap(war[o][i - 1], war[o][i]);
}
anum[o]--;
}
void fight(int o1,int o2) {
war[o1][attacker].health -= war[o2][defender].attack;
war[o2][defender].health -= war[o1][attacker].attack;
if (war[o1][attacker].health <= 0) clear(o1,attacker);
if (war[o2][defender].health <= 0) clear(o2, defender);
}
void output(int o) {
cout << war[o][0].health << endl;
cout << anum[o];
for (int i = 1; i <= anum[o]; i++) cout << " " << war[o][i].health;
cout << endl;
}
int main() {
cin >> n;
anum[0] = anum[1] = 0;
war[0][0].attack = war[1][0].attack = 0;
war[0][0].health = war[1][0].health = 30;
while (n--) {
string s;
cin >> s;
if (s == "summon") {
cin >> position >> attack >> health;
insert(order%2);
}
else if (s == "attack") {
cin >> attacker >> defender;
fight(order%2,(order+1)%2);
}
else {
order++;
}
if (war[0][0].health > 30 || war[1][0].health > 30) {
cout << "error" << endl;
}
}
if (war[0][0].health <= 0) {
cout << "-1" << endl;
}
else if (war[1][0].health <= 0) {
cout << "1" << endl;
}
else cout << "0" << endl;
output(0);
output(1);
}
tip:
1.如果是hero死亡了,那么不要移动hero的位置
2.NMAX的值应该设置为8,而不应该是7,否则会产生溢出