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

news2025/6/7 2:07:08

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

#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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2402340.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LabVIEW与Modbus/TCP温湿度监控系统

基于LabVIEW 开发平台与 Modbus/TCP 通信协议&#xff0c;设计一套适用于实验室环境的温湿度数据采集监控系统。通过上位机与高精度温湿度采集设备的远程通信&#xff0c;实现多设备温湿度数据的实时采集、存储、分析及报警功能&#xff0c;解决传统人工采集效率低、环境适应性…

Cursor 1.0 版本 GitHub MCP 全面指南:从安装到工作流增强

Cursor 1.0 版本 GitHub MCP 全面指南:从安装到工作流增强 简介 GitHub MCP (Machine Coding Protocol) 是一种强大的工具,能够自动化代码生成、管理和分析,从而显著提升开发效率。本文将全面介绍 GitHub MCP 的安装、配置、使用以及如何将其融入您的工作流。 本文介绍两种…

自主设计一个DDS信号发生器

DDS发生器 DDS信号发生器是直接数字频率合成技术&#xff0c;采用直接数字频率合成(Direct Digital Synthesis&#xff0c;简称DDS)技术&#xff0c;把信号发生器的频率稳定度、准确度提高到与基准频率相同的水平&#xff0c;并且可以在很宽的频率范围内进行精细的频率调节。采…

鸿蒙UI(ArkUI-方舟UI框架)- 使用弹框

返回主章节 → 鸿蒙UI&#xff08;ArkUI-方舟UI框架&#xff09; 文章目录 弹框概述使用弹出框(Dialog)弹出框概述不依赖UI组件的全局自定义弹出框(openCustomDialog)(推荐)生命周期自定义弹出框的打开与关闭更新自定义弹出框内容更新自定义弹出框的属性完整示例 基础自定义弹…

学习笔记(24): 机器学习之数据预处理Pandas和转换成张量格式[2]

学习笔记(24): 机器学习之数据预处理Pandas和转换成张量格式[2] 学习机器学习&#xff0c;需要学习如何预处理原始数据&#xff0c;这里用到pandas&#xff0c;将原始数据转换为张量格式的数据。 学习笔记(23): 机器学习之数据预处理Pandas和转换成张量格式[1]-CSDN博客 下面…

在不同型号的手机或平板上后台运行Aidlux

在不同型号的手机或平板上后台运行Aidlux 一、鸿蒙/HarmonyOS手机与平板 二、小米手机与平板 三、OPPO手机与平板 四、vivo手机与平板 一、鸿蒙/HarmonyOS手机与平板 &#xff08;系统版本有差异&#xff0c;但操作原理相通&#xff09; 第一步&#xff1a;点击设置——应用和…

【SSM】SpringBoot学习笔记1:SpringBoot快速入门

前言&#xff1a; 文章是系列学习笔记第9篇。基于黑马程序员课程完成&#xff0c;是笔者的学习笔记与心得总结&#xff0c;供自己和他人参考。笔记大部分是对黑马视频的归纳&#xff0c;少部分自己的理解&#xff0c;微量ai解释的内容&#xff08;ai部分会标出&#xff09;。 …

1.企业可观测性监控三大支柱及开源方案的横评对比

[ 知识是人生的灯塔&#xff0c;只有不断学习&#xff0c;才能照亮前行的道路 ] &#x1f4e2; 大家好&#xff0c;我是 WeiyiGeek&#xff0c;一名深耕安全运维开发&#xff08;SecOpsDev&#xff09;领域的技术从业者&#xff0c;致力于探索DevOps与安全的融合&#xff08;De…

双空间知识蒸馏用于大语言模型

Dual-Space Knowledge Distillation for Large Language Models 发表&#xff1a;EMNLP 2024 机构&#xff1a;Beijing Key Lab of Traffic Data Analysis and Mining 连接&#xff1a;https://aclanthology.org/2024.emnlp-main.1010.pdf 代码&#xff1a;GitHub - songmz…

OpenCV CUDA模块特征检测------角点检测的接口createMinEigenValCorner()

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 该函数创建一个 基于最小特征值&#xff08;Minimum Eigenvalue&#xff09;的角点响应计算对象&#xff0c;这是另一种经典的角点检测方法&…

8天Python从入门到精通【itheima】-69~70(字符串的常见定义和操作+案例练习)

目录 69节-字符串的定义和操作 1.学习目标 2.数据容器视角下的字符串 3.字符串的下标索引 4.字符串是一个无法修改的数据容器 5.字符串的常用操作 【1】index方法 【2】replace方法&#xff1a;进过替换&#xff0c;得到一个新的字符串 【3】split方法&#xff1a;将字…

GC1809:高性能音频接收与转换芯片

GC1809 是一款高性能音频接收与转换芯片&#xff0c;适用于多种音频设备&#xff0c;如 A/V 接收器、多媒体音响设备、机顶盒等。本文将简要介绍该芯片的主要特性、性能参数及应用。 主要特性 多协议兼容&#xff1a;兼容 IEC60958、S/PDIF、EIAJ CP1201 和 AES3 协议。 多种…

项目实战——C语言扫雷游戏

这是一款9*9的扫雷游戏 扫雷游戏 1.需求分析2.程序框架设计3.分函数实现打印游戏菜单界面游戏主逻辑函数程序主入口初始化游戏棋盘随机布置地雷显示当前棋盘状态计算指定位置周围的地雷数量玩家排雷主逻辑 4.分文件实现&#xff08;1&#xff09;test.c&#xff08;2&#xff0…

【Java】CopyOnWriteArrayList

一&#xff0c;概述 CopyOnWriteArrayList作为List接口的实现之一&#xff0c;它区分于ArrayList在于它是线程安全的。如它名字一样&#xff0c;所有的写操作均复制了原数组的值&#xff0c;虽说代价较大&#xff0c;但读多写少的环境下&#xff0c;是可接受的。笔者在此简单看…

C#入门学习笔记 #8(委托)

欢迎进入这篇文章,文章内容为学习C#过程中做的笔记,可能有些内容的逻辑衔接不是很连贯,但还是决定分享出来,由衷的希望可以帮助到你。 笔记内容会持续更新~~ 本章介绍C#中的委托,本章难度较大... 委托 C#中的委托是C语言、C++中函数指针的升级版。接下来介绍一个概念—…

CSS 3D 变换中z-index失效问题

CSS 3D 变换中 z-index 失效问题 1. z-index 失效了 在 CSS 中&#xff0c;z-index 通常用于控制元素的层叠顺序&#xff0c;数值越大&#xff0c;元素越靠前显示。在 3D 变换&#xff08;如 rotateX、translateZ&#xff09; 中使用 z-index 时&#xff0c;可能会发现z-inde…

Tailwind CSS 实战:基于 Kooboo 构建 AI 对话框页面(七):消息框交互功能添加

Tailwind CSS 实战&#xff0c;基于Kooboo构建AI对话框页面&#xff08;一&#xff09; Tailwind CSS 实战&#xff0c;基于Kooboo构建AI对话框页面&#xff08;二&#xff09;&#xff1a;实现交互功能 Tailwind CSS 实战&#xff0c;基于 Kooboo 构建 AI 对话框页面&#x…

【计算机网络】网络层IP协议与子网划分详解:从主机通信到网络设计的底层逻辑

&#x1f525;个人主页&#x1f525;&#xff1a;孤寂大仙V &#x1f308;收录专栏&#x1f308;&#xff1a;计算机网络 &#x1f339;往期回顾&#x1f339;&#xff1a; 【计算机网络】传输层TCP协议——协议段格式、三次握手四次挥手、超时重传、滑动窗口、流量控制、 &…

基于WSL搭建Ubnutu 20.04.6 LTS(二)-部署Docker环境

Docker是一组平台即服务&#xff08;PaaS&#xff09;的产品。它基于操作系统层级的虚拟化技术&#xff0c;将软件与其依赖项打包为容器。托管容器的软件称为Docker引擎。Docker能够帮助开发者在轻量级容器中自动部署应用程序&#xff0c;并使得不同容器中的应用程序彼此隔离&a…

【图像处理入门】6. 频域图像处理:傅里叶变换与滤波的奥秘

摘要 频域图像处理通过傅里叶变换将图像从空间域转换到频率域,为图像增强、去噪、压缩等任务提供全新视角。本文将深入解析傅里叶变换原理,介绍低通、高通滤波的实现方式,结合OpenCV和Python代码展示频域滤波在去除噪声、增强边缘中的应用,帮助读者掌握图像频域处理的核心…