炉石传说 第八次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;
}
相关推荐
你撅嘴真丑11 小时前
第九章-数字三角形
算法
在路上看风景11 小时前
19. 成员初始化列表和初始化对象
c++
uesowys11 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
zmzb010311 小时前
C++课后习题训练记录Day98
开发语言·c++
ValhallaCoder11 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮11 小时前
AI 视觉连载1:像素
算法
念风零壹12 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
智驱力人工智能12 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥12 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风13 小时前
代码随想录第十五天
数据结构·算法·leetcode