CSP-201609-3炉石传说

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值