http://118.190.20.162/view.page?gpid=T45
思路:
- 首先构建结构体role用来保存英雄/随从,包含生命值和攻击力两个变量,这样两名玩家分别用
vector<role>
表示。 - 使用变量pl表示当前回合的玩家,每回合结束进行转换:
0 <-> 1
。 - 输入字符串,判断操作类型,之后进行相应的处理:
summon
:直接调用vector里面的插入即可;attack
:双方血量减少;并且判断攻击方血量是否<=0,如果是那么直接erase;如果被攻击的不是英雄并且血量<=0那么直接erase;end
:转换玩家,进入下一回合。
实现:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct role {
int health;
int attack;
role(int h, int a) : health(h), attack(a) {}
};
vector<role> player[2];
int main() {
int N;
cin >> N;
int pl = 0;
player[0].push_back(role(30, 0));
player[1].push_back(role(30, 0));
for(int n=0; n<N; n++) {
string type;
cin >> type;
if(type == "summon") {
int pos, h, a;
cin >> pos >> a >> h;
player[pl].insert(player[pl].begin()+pos, role(h, a));
}
else if(type == "attack") {
int att, deff;
cin >> att >> deff;
player[pl][att].health -= player[!pl][deff].attack;
player[!pl][deff].health -= player[pl][att].attack;
if(player[pl][att].health <= 0) {
player[pl].erase(player[pl].begin()+att);
}
if(player[!pl][deff].health <= 0 && deff!=0) {
player[!pl].erase(player[!pl].begin()+deff);
}
}
else if(type == "end") {
pl = !pl;
}
}
if(player[0][0].health>0 && player[1][0].health>0) cout << 0 << endl;
else if(player[0][0].health>0) cout << 1 << endl;
else cout << -1 << endl;
for(int i=0; i<2; i++) {
cout << player[i][0].health << endl;
cout << player[i].size()-1 << " ";
for(int j=1; j<player[i].size(); j++) {
cout << player[i][j].health << " ";
}
cout << endl;
}
}