CCF Hearthstone full score code (detailed notes) + problem-solving ideas (structural simulation) 201609-3

topic description

insert image description here

problem solving ideas

Use a structure to store life and attack power for each person Use
a structure two-dimensional array to store all personnel information
p[0,0] stores the first hero, p[0,1~7] stores the first follower
p[1,0] Store the first-hand hero, p[1,1~7] stores the first-hand follower

Read in n operations, execute these n operations, and finally output the life information of all personnel

Sommon operation, move the person behind the pos position to the right, and then insert the new follower into the pos position
attack operation, subtract the opponent's attack value from the life of both parties, if the person is dead and not a hero, move the follower behind one place forward
If it is an end operation, exchange the operation objects of both parties (r = !r)


Code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <stack>

using namespace std;

struct people
{
    
    
    int a;
    int h;
}p[2][8]; //0为先手

void lmove(int i, int pos) //将随从左移
{
    
    
    for (int j = pos; j < 7; j ++) p[i][j] = p[i][j + 1];
    p[i][7] = {
    
    0, 0}; //最后一个空位进行清空
}

void print(int m) //输出
{
    
    
    cout << p[m][0].h << endl; //输出英雄生命
    
    int cnt = 0;
    for (int i = 1; i <= 7; i ++)
    {
    
    
        if (p[m][i].h > 0) cnt ++;
        else break;
    }
    cout << cnt;//输出存活的随从个数
    
    for (int i = 1; i <= cnt; i ++) cout << " " << p[m][i].h;
    cout << endl;
}

int main()
{
    
    
    int n;
    cin >> n;

    //初始化
    p[0][0] = {
    
    0, 30};
    p[1][0] = {
    
    0, 30};

    int r = 0; //定义先手为0,对手为!r
    while (n --)
    {
    
    
        string op;
        cin >> op;

        if (op == "summon") //召唤随从
        {
    
    
            int pos, atk, heal;
            cin >> pos >> atk >> heal;
            
            for (int j = 7; j > pos; j --) p[r][j] = p[r][j - 1]; //右移
            p[r][pos] = {
    
    atk, heal}; //插入新增的随从
        }
        else if (op == "attack") //攻击
        {
    
    
            int atker, defender;
            cin >> atker >> defender;
            
            //攻击方和被攻击方会同时对彼此造成等同于自己攻击力的伤害
            p[r][atker].h -= p[!r][defender].a;
            p[!r][defender].h -= p[r][atker].a;
            
            //如果任一方(不是英雄的情况下)死了,将后面的随从左移
            if (p[r][atker].h <= 0) lmove(r, atker);
            if (defender != 0 && p[!r][defender].h <= 0) lmove(!r, defender);
        }
        else
        {
    
    
            r = !r; //交换操作方
        }
    }

    if (p[0][0].h <= 0) cout << -1 << endl; //先手死了
    else if (p[1][0].h <= 0) cout << 1 << endl; //对手死了
    else cout << 0 << endl; //两个都没死

    //输出双方人员情况
    print(0);
    print(1);

    return 0;
}

Guess you like

Origin blog.csdn.net/qq_51800570/article/details/129208905