炉石传说 第八次CCF-CSP计算机软件能力认证

纯链表模拟,各种操作熟知就很简单

cpp 复制代码
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int n;

struct role {
    int attack;
    int health;
    struct role* next;

    role() : attack(0), health(0), next(nullptr) {}
    role(int attack, int health) : attack(attack), health(health), next(nullptr) {}

    // 在指定位置插入新节点
    static role* insert(role* head, int position, int attack, int health) {
        role* new_node = new role(attack, health);

        // 插入到头部
        if (position == 1) {
            new_node->next = head;
            return new_node;
        }

        // 插入到中间或尾部
        role* current = head;
        for (int i = 1; i < position - 1 && current != nullptr; i++) {
            current = current->next;
        }

        if (current != nullptr) {
            new_node->next = current->next;
            current->next = new_node;
        }

        return head;
    }

    // 删除指定位置的节点
    static role* remove(role* head, int position) {
        if (head == nullptr) return nullptr;

        // 删除头节点
        if (position == 1) {
            role* temp = head;
            head = head->next;
            delete temp;
            return head;
        }

        // 删除中间或尾部节点
        role* current = head;
        for (int i = 1; i < position - 1 && current->next != nullptr; i++) {
            current = current->next;
        }

        if (current->next != nullptr) {
            role* temp = current->next;
            current->next = temp->next;
            delete temp;
        }

        return head;
    }

    // 获取指定位置的节点
    static role* get(role* head, int position) {
        role* current = head;
        for (int i = 1; i < position && current != nullptr; i++) {
            current = current->next;
        }
        return current;
    }

    // 获取链表长度
    static int size(role* head) {
        int count = 0;
        role* current = head;
        while (current != nullptr) {
            count++;
            current = current->next;
        }
        return count;
    }

    // 清理死亡的随从
    static role* cleanup(role* head) {
        while (head != nullptr && head->health <= 0) {
            role* temp = head;
            head = head->next;
            delete temp;
        }

        if (head == nullptr) return nullptr;

        role* current = head;
        while (current->next != nullptr) {
            if (current->next->health <= 0) {
                role* temp = current->next;
                current->next = temp->next;
                delete temp;
            }
            else {
                current = current->next;
            }
        }

        return head;
    }
};

struct gamer {
    struct role hero;
    struct role* helper;

    gamer() : helper(nullptr) {}
    gamer(int attack, int health) : hero(attack, health), helper(nullptr) {}

    int getSize() {
        return role::size(helper);
    }
};

struct chessboard {
    struct gamer player[2];
} cb;

void init() {
    cb.player[0] = gamer(0, 30);
    cb.player[1] = gamer(0, 30);
}

// 召唤随从
void summon(int index, int position, int attack, int health) {
    cb.player[index].helper = role::insert(cb.player[index].helper, position, attack, health);
}

// 攻击操作
bool attack(int index, int attacker, int defender) {
    int d_index = index ^ 1;

    // 获取攻击者
    role* attacker_role = role::get(cb.player[index].helper, attacker);
    if (attacker_role == nullptr) return false;

    int attack_power = attacker_role->attack;

    if (defender == 0) {
        // 攻击对方英雄
        cb.player[d_index].hero.health -= attack_power;
        attacker_role->health -= cb.player[d_index].hero.attack;

        // 清理死亡的随从
        cb.player[index].helper = role::cleanup(cb.player[index].helper);

        // 检查英雄是否死亡
        if (cb.player[d_index].hero.health <= 0) {
            return true;
        }
    }
    else {
        // 攻击对方随从
        role* target = role::get(cb.player[d_index].helper, defender);
        if (target != nullptr) {
            target->health -= attack_power;
            attacker_role->health -= target->attack;

            // 清理死亡的随从
            cb.player[index].helper = role::cleanup(cb.player[index].helper);
            cb.player[d_index].helper = role::cleanup(cb.player[d_index].helper);
        }
    }

    return false;
}

// 输出当前状态
void printState() {
    // 检查游戏结果
    int result = 0;
    if (cb.player[0].hero.health <= 0) result = -1;
    else if (cb.player[1].hero.health <= 0) result = 1;

    cout << result << endl;

    // 输出先手玩家信息
    cout << cb.player[0].hero.health << endl;
    cout << cb.player[0].getSize();
    role* current = cb.player[0].helper;
    while (current != nullptr) {
        cout << " " << current->health;
        current = current->next;
    }
    cout << endl;

    // 输出后手玩家信息
    cout << cb.player[1].hero.health << endl;
    cout << cb.player[1].getSize();
    current = cb.player[1].helper;
    while (current != nullptr) {
        cout << " " << current->health;
        current = current->next;
    }
    cout << endl;
}

int main() {
    cin >> n;
    init();
    int index = 0; // 先手

    while (n--) {
        string action;
        cin >> action;

        if (action == "summon") {
            int position, attack, health;
            cin >> position >> attack >> health;
            summon(index, position, attack, health);
        }
        else if (action == "attack") {
            int attacker, defender;
            cin >> attacker >> defender;
            if (attack(index, attacker, defender)) {
                // 游戏结束
                break;
            }
        }
        else if (action == "end") {
            index = index ^ 1;
        }
    }

    printState();
    return 0;
}
相关推荐
Morwit2 分钟前
Qt qml创建c++类的单例对象
开发语言·c++·qt
June`2 分钟前
IO模型全解析:从阻塞到异步(高并发的reactor模型)
linux·服务器·网络·c++
YxVoyager8 分钟前
Qt C++ :QRegularExpression 正则表达式使用详解
c++·qt·正则表达式
闻缺陷则喜何志丹9 分钟前
【回文 字符串】3677 统计二进制回文数字的数目|2223
c++·算法·字符串·力扣·回文
李余博睿(新疆)11 分钟前
c++分治算法
c++
Tisfy16 分钟前
LeetCode 0085.最大矩形:单调栈
算法·leetcode·题解·单调栈
oioihoii16 分钟前
Protocol Buffers 编码原理深度解析
c++
消失的旧时光-194317 分钟前
函数指针 + 结构体 = C 语言的“对象模型”?——从 C 到 C++ / Java 的本质统一
linux·c语言·开发语言·c++·c
mit6.82418 分钟前
出入度|bfs|状压dp
算法
hweiyu0018 分钟前
强连通分量算法:Kosaraju算法
算法·深度优先