炉石传说
题目
思路
这题比较直白,就是要仔细看题。。。attacker攻击对方随从时不是hp减去自己的攻击值,而是减去对方的攻击值。
使用vector,刚好英雄在0号位,插入删除和这题十分契合。
提交结果
代码
#include <iostream>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
struct player {
int hp, atk;
player() {}
player(int hp_, int atk_) :hp(hp_), atk(atk_) {}
};
vector<player> p[2];
int cnt, pos_, atk_, atker_, dfer_, hp_, flag = 0;
void option_summon(int pos, int hp, int atk, int x) {
p[x].insert(p[x].begin() + pos, { hp,atk });
}
void option_atk(int atker, int dfer, int x) {
int y = (x + 1) % 2;
p[x][atker].hp -= p[y][dfer].atk;
p[y][dfer].hp -= p[x][atker].atk;
if (p[x][atker].hp <= 0) p[x].erase(p[x].begin() + atker);
if (dfer!=0 && p[y][dfer].hp <= 0) p[y].erase(p[y].begin() + dfer);
}
int main() {
cin >> cnt;
p[0].push_back({ 30,0 });
p[1].push_back({ 30,0 });
for (int kkk = 0; kkk < cnt; kkk++) {
string option;
cin >> option;
if (option == "summon") {
cin >> pos_ >> atk_ >> hp_;
option_summon(pos_, hp_, atk_, flag);
}
else if (option == "attack") {
cin >> atker_ >> dfer_;
option_atk(atker_, dfer_, flag);
}
else if (option == "end") flag = (flag + 1) % 2;
}
if (p[1][0].hp <= 0) cout << 1 << endl;
else if (p[0][0].hp <= 0) cout << -1 << endl;
else cout << 0 << endl;
cout << p[0][0].hp<<endl;
cout << p[0].size() - 1 << ' ';
for (int i = 1; i <= p[0].size() - 1; i++) {
cout << p[0][i].hp;
if (i != p[0].size() - 1) cout << ' ';
}
cout << endl;
cout << p[1][0].hp<<endl;
cout << p[1].size() - 1 << ' ';
for (int i = 1; i <= p[1].size() - 1; i++) {
cout << p[1][i].hp;
if (i != p[1].size() - 1) cout << ' ';
}
cout << endl;
return 0;
}