CCF 201609-3 炉石传说

本文深入解析了一段关于炉石传说模拟战斗的C++代码,该代码旨在模拟游戏中的召唤、攻击和结束回合等操作。作者通过实战分享了编码过程中遇到的难题及部分解决方案,特别注意操作数可能为零的情况。尽管代码得分90分,但仍存在未知的10分bug待解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

201609-3 炉石传说 传送门

纯粹的模拟题, 按照规则一步步来模拟即可, 然而辣鸡的我之得了90分, 找了好久没有找出那10分的bug.

要注意的一点是: 操作数可能为0

还是贴一贴90分的辣鸡代码, 毕竟是一个一个敲出来的

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct Man {
    int position, attack, health;
    Man(int p, int a, int h) : position(p), attack(a), health(h) {}
};
vector<Man> man[2];

bool cmp(Man x, Man y)
{
    return x.position < y.position;
}

void show(vector<Man> man[2])
{
    sort(man[0].begin() + 1, man[0].end(), cmp);
    sort(man[1].begin() + 1, man[1].end(), cmp);
    cout << man[0][0].health << endl << man[0].size() - 1;
    for (int i = 1; i < man[0].size(); ++i)
        cout << ' ' << man[0][i].health;
    cout << endl << man[1][0].health << endl << man[1].size() - 1;
    for (int i = 1; i < man[1].size(); ++i)
        cout << ' ' << man[1][i].health;
    cout << endl;
}

int main()
{
    man[0].push_back(Man(0, 0, 30));
    man[1].push_back(Man(0, 0, 30));
    int n, time = 0, me = 0, you = 1;
    cin >> n;
    while (time != n) {
        string cmd;
        while (cin >> cmd) {
            time++;
            if (cmd == "summon") {
                int position, attack, health;
                cin >> position >> attack >> health;
                for (int i = 1; i < man[me].size(); ++i)
                    if (man[me][i].position >= position) man[me][i].position++;
                man[me].push_back(Man(position, attack, health));
            } else if (cmd == "attack") {
                int att_me, att_you, m, y;
                cin >> att_me >> att_you;
                for (int i = 1; i < man[me].size(); ++i)
                    if (man[me][i].position == att_me) m = i;
                for (int i = 0; i < man[you].size(); ++i)
                    if (man[you][i].position == att_you) y = i;
                man[me][m].health -= man[you][y].attack;
                man[you][y].health -= man[me][m].attack;
                if (man[me][m].health <= 0) {
                    man[me].erase(man[me].begin() + m);
                    for (int i = 1; i < man[me].size(); ++i) {
                        if (man[me][i].position > att_me) man[me][i].position--;
                    }
                }
                if (att_you != 0 && man[you][att_you].health <= 0) {
                    man[you].erase(man[you].begin() + y);
                    for (int i = 1; i < man[you].size(); ++i) {
                        if (man[you][i].position > att_you) man[you][i].position--;
                    }
                }
                if (att_you == 0 && man[you][0].health <= 0) {
                    cout << (me == 0 ? 1 : -1) << endl;
                    show(man);
                    return 0;
                }
            } else if (cmd == "end") break;
            if (time == n) {
                cout << 0 << endl;
                show(man);
                return 0;
            }
        }
        swap(me, you);
    }
    cout << 0 << endl;
    show(man);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值