CCF炉石传说

来源:互联网 发布:中国季度gdp数据 编辑:程序博客网 时间:2024/04/29 21:13

问题描述

  《炉石传说:魔兽英雄传》(Hearthstone: Heroes of Warcraft,简称炉石传说)是暴雪娱乐开发的一款集换式卡牌游戏(如下图所示)。游戏在一个战斗棋盘上进行,由两名玩家轮流进行操作,本题所使用的炉石传说游戏的简化规则如下:

  * 玩家会控制一些角色,每个角色有自己的生命值和攻击力。当生命值小于等于 0 时,该角色死亡。角色分为英雄和随从。
  * 玩家各控制一个英雄,游戏开始时,英雄的生命值为 30,攻击力为 0。当英雄死亡时,游戏结束,英雄未死亡的一方获胜。
  * 玩家可在游戏过程中召唤随从。棋盘上每方都有 7 个可用于放置随从的空位,从左到右一字排开,被称为战场。当随从死亡时,它将被从战场上移除。
  * 游戏开始后,两位玩家轮流进行操作,每个玩家的连续一组操作称为一个回合。
  * 每个回合中,当前玩家可进行零个或者多个以下操作:
  1) 召唤随从:玩家召唤一个随从进入战场,随从具有指定的生命值和攻击力。
  2) 随从攻击:玩家控制自己的某个随从攻击对手的英雄或者某个随从。
  3) 结束回合:玩家声明自己的当前回合结束,游戏将进入对手的回合。该操作一定是一个回合的最后一个操作。
  * 当随从攻击时,攻击方和被攻击方会同时对彼此造成等同于自己攻击力的伤害。受到伤害的角色的生命值将会减少,数值等同于受到的伤害。例如,随从 X 的生命值为 HX、攻击力为 AX,随从 Y 的生命值为 HY、攻击力为 AY,如果随从 X 攻击随从 Y,则攻击发生后随从 X 的生命值变为 HX - AY,随从 Y 的生命值变为 HY - AX。攻击发生后,角色的生命值可以为负数。
  本题将给出一个游戏的过程,要求编写程序模拟该游戏过程并输出最后的局面。
输入格式
  输入第一行是一个整数 n,表示操作的个数。接下来 n 行,每行描述一个操作,格式如下:
  <action> <arg1> <arg2> ...
  其中<action>表示操作类型,是一个字符串,共有 3 种:summon表示召唤随从,attack表示随从攻击,end表示结束回合。这 3 种操作的具体格式如下:
  * summon <position> <attack> <health>:当前玩家在位置<position>召唤一个生命值为<health>、攻击力为<attack>的随从。其中<position>是一个 1 到 7 的整数,表示召唤的随从出现在战场上的位置,原来该位置及右边的随从都将顺次向右移动一位。
  * attack <attacker> <defender>:当前玩家的角色<attacker>攻击对方的角色 <defender>。<attacker>是 1 到 7 的整数,表示发起攻击的本方随从编号,<defender>是 0 到 7 的整数,表示被攻击的对方角色,0 表示攻击对方英雄,1 到 7 表示攻击对方随从的编号。
  * end:当前玩家结束本回合。
  注意:随从的编号会随着游戏的进程发生变化,当召唤一个随从时,玩家指定召唤该随从放入战场的位置,此时,原来该位置及右边的所有随从编号都会增加 1。而当一个随从死亡时,它右边的所有随从编号都会减少 1。任意时刻,战场上的随从总是从1开始连续编号。
输出格式
  输出共 5 行。
  第 1 行包含一个整数,表示这 n 次操作后(以下称为 T 时刻)游戏的胜负结果,1 表示先手玩家获胜,-1 表示后手玩家获胜,0 表示游戏尚未结束,还没有人获胜。
  第 2 行包含一个整数,表示 T 时刻先手玩家的英雄的生命值。
  第 3 行包含若干个整数,第一个整数 p 表示 T 时刻先手玩家在战场上存活的随从个数,之后 p 个整数,分别表示这些随从在 T 时刻的生命值(按照从左往右的顺序)。
  第 4 行和第 5 行与第 2 行和第 3 行类似,只是将玩家从先手玩家换为后手玩家。
样例输入
8
summon 1 3 6
summon 2 4 2
end
summon 1 4 5
summon 1 2 1
attack 1 2
end
attack 1 1
样例输出
0
30
1 2
30
1 2
样例说明
  按照样例输入从第 2 行开始逐行的解释如下:
  1. 先手玩家在位置 1 召唤一个生命值为 6、攻击力为 3 的随从 A,是本方战场上唯一的随从。
  2. 先手玩家在位置 2 召唤一个生命值为 2、攻击力为 4 的随从 B,出现在随从 A 的右边。
  3. 先手玩家回合结束。
  4. 后手玩家在位置 1 召唤一个生命值为 5、攻击力为 4 的随从 C,是本方战场上唯一的随从。
  5. 后手玩家在位置 1 召唤一个生命值为 1、攻击力为 2 的随从 D,出现在随从 C 的左边。
  6. 随从 D 攻击随从 B,双方均死亡。
  7. 后手玩家回合结束。
  8. 随从 A 攻击随从 C,双方的生命值都降低至 2。
评测用例规模与约定
  * 操作的个数0 ≤ n ≤ 1000。
  * 随从的初始生命值为 1 到 100 的整数,攻击力为 0 到 100 的整数。
  * 保证所有操作均合法,包括但不限于:
  1) 召唤随从的位置一定是合法的,即如果当前本方战场上有 m 个随从,则召唤随从的位置一定在 1 到 m + 1 之间,其中 1 表示战场最左边的位置,m + 1 表示战场最右边的位置。
  2) 当本方战场有 7 个随从时,不会再召唤新的随从。
  3) 发起攻击和被攻击的角色一定存在,发起攻击的角色攻击力大于 0。
  4) 一方英雄如果死亡,就不再会有后续操作。
  * 数据约定:
  前 20% 的评测用例召唤随从的位置都是战场的最右边。
  前 40% 的评测用例没有 attack 操作。
  前 60% 的评测用例不会出现随从死亡的情况。
三战炉石传说代码:
#include<iostream>#include<vector>using namespace std;struct Role{int health;int attack;Role(int a,int b){health = a;attack = b;}};vector<Role> player[2];int main(){int n,i,j,type = 1;string action;player[0].push_back(Role(30,0));player[1].push_back(Role(30,0)); cin>>n;while(n-- && player[0].at(0).health > 0 && player[1].at(0).health >0){cin>>action;if(action == "summon"){int position,attack,health;cin>>position>>attack>>health;if(type == 1){player[0].insert(player[0].begin()+position,Role(health,attack));}else{player[1].insert(player[1].begin()+position,Role(health,attack));}}else if(action == "attack"){int attacker,defender;cin>>attacker>>defender;if(type == 1){player[0].at(attacker).health -= player[1].at(defender).attack;player[1].at(defender).health -= player[0].at(attacker).attack;if(player[0].at(attacker).health <= 0 && attacker != 0){player[0].erase(player[0].begin()+attacker);}if(player[1].at(defender).health <= 0 && defender != 0){player[1].erase(player[1].begin()+defender);}}else{player[1].at(attacker).health -= player[0].at(defender).attack;player[0].at(defender).health -= player[1].at(attacker).attack;if(player[1].at(attacker).health <= 0 && attacker != 0){player[1].erase(player[1].begin()+attacker);}if(player[0].at(defender).health <= 0 && defender != 0){player[0].erase(player[0].begin()+defender);}}}else if(action == "end"){type = -type;}}if(player[0].at(0).health <= 0 && player[1].at(0).health > 0){cout<<-1<<endl;}if(player[0].at(0).health > 0 && player[1].at(0).health <= 0){cout<<1<<endl;}if(player[0].at(0).health > 0 && player[1].at(0).health > 0){cout<<0<<endl;}if(player[0].at(0).health <= 0 && player[1].at(0).health <= 0){cout<<0<<endl;}for(i = 0; i < 2; i++){cout<<player[i].at(0).health<<endl;//输出英雄的血量 cout<<player[i].size()-1<<" "; for(j = 1; j < player[i].size(); j++){cout<<player[i].at(j).health<<" ";}cout<<endl;}return 0;} 


再战炉石传说AC代码:
#include<iostream>#include<vector>#include<windows.h>using namespace std;struct player{int health; int attack; player(int a,int b)  {      health = a;      attack = b;  } };vector<player> p1;vector<player> p2;void Summon(int pos,int a,int h,int cur){    if(cur == 1)    {    p1.insert(p1.begin()+pos,player(h,a));    }else if(cur == -1)    {        p2.insert(p2.begin()+pos,player(h,a));    }   }void Attack(int attacker,int defender,int cur){    if(cur == 1)    {        p1[attacker].health = p1[attacker].health - p2[defender].attack;        p2[defender].health = p2[defender].health - p1[attacker].attack;        if(p1[attacker].health <= 0 && attacker != 0)  //0号位放是英雄,英雄死亡时不能像随从一样删掉移位         {            p1.erase(p1.begin()+attacker);        }        if(p2[defender].health <= 0 && defender != 0)        {            p2.erase(p2.begin()+defender);        }    }else if(cur == -1)    {    p2[attacker].health = p2[attacker].health - p1[defender].attack;        p1[defender].health = p1[defender].health - p2[attacker].attack;        if(p2[attacker].health <= 0 && attacker != 0)        {            p2.erase(p2.begin()+attacker);        }        if(p1[defender].health <= 0 && defender != 0)        {            p1.erase(p1.begin()+defender);        }     }}int main(){    int n,cur,i;    p1.push_back(player(30,0));    p2.push_back(player(30,0));    cin>>n;    cur = 1;    while(n-- && p1[0].health > 0 && p2[0].health > 0)    {        string operation;        cin>>operation;        if(operation == "summon")        {            int p,a,h;            cin>>p>>a>>h;            Summon(p,a,h,cur);        }else if(operation == "attack")        {            int attacker,defender;            cin>>attacker>>defender;            Attack(attacker,defender,cur);        }else if(operation == "end")        {            cur = -cur;        }    }    if(p1[0].health > 0 && p2[0].health > 0)   // 都没死     {        cout<<0<<endl;    }else if(p1[0].health <= 0 && p2[0].health <= 0)  //同归于尽     {        cout<<0<<endl;       }else   // 死一个     {         if(p1[0].health <= 0)         {             cout<<-1<<endl;         }         if(p2[0].health <= 0)         {             cout<<1<<endl;         }    }     cout<<p1[0].health<<endl;    cout<<p1.size()-1;    vector<player>::iterator it;    for(it = p1.begin()+1; it != p1.end(); it++)    {        cout<<" "<<(*it).health;    }    cout<<endl;    cout<<p2[0].health<<endl;    cout<<p2.size()-1;    for(it = p2.begin()+1; it != p2.end(); it++)    {        cout<<" "<<(*it).health;    }    system("pause");    return 0;   } 
笔记:易错点有以下几个:
1、随从攻击时要考虑到随从死亡,且英雄死亡时不能更新位置
2、注意是先手获胜输出1,即先手玩家的血量<=0 时输出-1
3、英雄可能同归于尽。
使用vector动态数组去存放英雄和随从,比原来的固定数组方便很多,特别是召唤方法
vector的insert和erase方法的操作参数都是对迭代器指针,如:p1.insert(p1.begin()+pos,player(h,a)); p1.erase(p1.begin()+attacker);

----------------------------------------------------------以下可以不用看-----------------------------------------------------------
原AC代码:(CCF官网提交结果延时还是咋的,没有修改代码居然提交成功了,害我花了6个小时去找一个正确程序的BUG,真心无语!!!!)
#include <stdio.h>#include <string.h>#include<iostream>struct player{int health;int attack;int flag;};void Summon(struct player * a,int position,int attack,int health){if(a[position].flag == 0){for(int i = 1; i <= 7; i++){if(a[i].flag == 0){a[i].attack = attack;a[i].health = health;a[i].flag = 1;break;}}}else if(a[position].flag == 1){for(int i = 7; i > position; i--){if(a[i-1].flag == 1)a[i] = a[i-1];}a[position].attack = attack;a[position].health = health;a[position].flag = 1;}}void Attack(struct player * a,struct player * b,int mePos,int enemyPos){a[mePos].health = a[mePos].health - b[enemyPos].attack;b[enemyPos].health = b[enemyPos].health - a[mePos].attack;if(a[mePos].health <= 0 && mePos != 0){if(mePos == 7){a[mePos].flag = 0;}else{   for(int i = mePos; i <= 6; i++)   {   if(a[i+1].flag == 1)   {   a[i] = a[i+1];   }else   {   a[i].flag = 0;   break;   }   }}}if(b[enemyPos].health <= 0 && enemyPos != 0){if(enemyPos == 7){b[enemyPos].flag = 0;}else{    for(int i = enemyPos; i <= 6; i++){    if(b[i+1].flag == 1){   b[i] = b[i+1];}else{b[i].flag = 0;    break;}   }   }}}int main(){int n,i;int type = 1;int result = 0;char action[20];struct player first[8];struct player second[8];first[0].health = 30;first[0].attack = 0;first[0].flag = 1;second[0].health = 30;second[0].attack = 0;second[0].flag = 1;for(i = 1; i <= 7; i++){first[i].flag = 0;second[i].flag = 0;}scanf("%d",&n);while(n-- && first[0].health >0 && second[0].health > 0){scanf("%s",action);if(strcmp(action,"summon") == 0){int position,attack,health;scanf("%d%d%d",&position,&attack,&health);if(type == 1){Summon(first,position,attack,health);}else if(type == -1){Summon(second,position,attack,health);}}if(strcmp(action,"attack") == 0){int mePos,enemyPos;scanf("%d%d",&mePos,&enemyPos);if(type == 1)Attack(first,second,mePos,enemyPos);if(type == -1)Attack(second,first,mePos,enemyPos);}if(strcmp(action,"end") == 0){type = -type;}}if(first[0].health <= 0){result = -1;}if(second[0].health <= 0){result = 1;}printf("%d\n",result);printf("%d\n",first[0].health);int count1,count2;count1 = count2 = 0;int hp1[8],hp2[8];memset(hp1,0,sizeof(hp1));memset(hp2,0,sizeof(hp2));for(i = 1; i <= 7; i++){if(first[i].flag == 1){count1++;hp1[i] = first[i].health;}if(second[i].flag == 1){count2++;hp2[i] = second[i].health;}}printf("%d",count1);for(i = 1; i<= 7; i++){if(hp1[i] > 0)printf(" %d",hp1[i]);}printf("\n");printf("%d\n",second[0].health);printf("%d",count2);for(i = 1; i <= 7; i++){if(hp2[i] > 0)printf(" %d",hp2[i]);}return 0;}







1 0
原创粉丝点击