CCF 炉石传说
2017-03-20 19:45
134 查看
这是我在CCF认证上面做的一个练习题,也是16年9月份的考题。
问题描述
《炉石传说:魔兽英雄传》(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 行,每行描述一个操作,格式如下:
…
其中表示操作类型,是一个字符串,共有 3 种:summon表示召唤随从,attack表示随从攻击,end表示结束回合。这 3 种操作的具体格式如下:
* summon :当前玩家在位置召唤一个生命值为、攻击力为的随从。其中是一个 1 到 7 的整数,表示召唤的随从出现在战场上的位置,原来该位置及右边的随从都将顺次向右移动一位。
* attack :当前玩家的角色攻击对方的角色 。是 1 到 7 的整数,表示发起攻击的本方随从编号,是 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% 的评测用例不会出现随从死亡的情况。
这个我只拿了80分,一共100分。还没想明白那20分的测试用例是怎么没有通过的。现在看看我通过80%用例的代码吧!
其实这个题的算法倒是不难,主要是写起来很麻烦,而且对这整个游戏规则的逻辑需要了解得很清晰,不然就容易写着写着就乱了。
首先是设定几个命令对应的常量:
string Summon = "summon"; string Attack = "attack"; string End = "end";
然后为了使用方便,定义随从的类:
class suicong { public: int attackBility;//攻击力 int live;//生命值 suicong(int atttack = 0, int life = 0) :attackBility(atttack), live(life) { } suicong() {} }; vector<suicong> player[2];
然后输入交手次数后,就是执行输入的命令,用turn的0和1来表示先手和后手(默认先是先手出招),含两个suicong元素的vector数组player来记录各个英雄的随从。
如果是传唤命令,将随从插入指定的位置。此处用vector实现,如果是插入在最后,就直接用push_back; 如果是插入在中间,就用insert。
if (command == Summon) { int attack = 0, life = 0, positon; cin >> positon >> attack >> life; if (positon > player[turn].size())//若插入在最后 player[turn].push_back(suicong(attack, life)); else //插入在中间 player[turn].insert(player[turn].begin() + positon - 1, suicong(attack, life)); }
如果命令是攻击,判断攻击的是对方英雄还是随从。注意(这个地方当时理解错了,导致结果出错),题目的意思是若攻击对方随从,对方的随从会受到自己的随从的攻击,同时自己的随从也会受到对方相随从的攻击,类似于交叉攻击。若是攻击对方的英雄,则只是对方的英雄受到攻击,自己的随从没事儿。
if (command == Attack) { int use = 0, to = 0; cin >> use >> to; if (!to) {//如果攻击对方玩家 if (turn) {//如果是二号玩家,攻击一号玩家 play1.live -= player[turn][use-1].attackBility; if (play1.live <= 0) break; } else {//如果是一号玩家,攻击二号玩家 play2.live -= player[turn][use-1].attackBility; if (play2.live <= 0) break; } } else//如果攻击对方随从 { if (!turn) {//一号攻击二号随从 player[turn+1][to - 1].live -= player[turn][use - 1].attackBility; player[turn][use - 1].live -= player[turn+1][to - 1].attackBility; if (player[turn+1][to - 1].live <= 0) player[turn+1].erase(player[turn+1].begin() + to - 1); } else//二号攻击一号随从 { player[turn - 1][to - 1].live -= player[turn][use - 1].attackBility; player[turn][use - 1].live -= player[turn-1][to - 1].attackBility; if (player[turn - 1][to - 1].live <= 0) player[turn - 1].erase(player[turn-1].begin() + to - 1); } } if (player[turn][use - 1].live <= 0) player[turn].erase(player[turn].begin() + use - 1); }
如果命令是结束回合,将turn的值变换就行:
if (command == End) { if (turn) turn = 0; else turn = 1; }
N个交手结束后,输出那个是赢家或者平手,再输出先手英雄剩余的生命值,随从个数和每个随从的生命值,后手也是一样:
if (!play2.live) cout << "1" << endl; if (!play1.live) cout << "-1" << endl; if (play1.live && play2.live) cout << "0" << endl; cout << play1.live << endl; cout << player[0].size(); for (item = player[0].begin(); item != player[0].end(); item++) { cout <<" "<< item->live; } cout << endl; cout << play2.live << endl; cout << player[1].size(); for (item = player[1].begin(); item != player[1].end(); item++) { cout << " " << item->live; } cout << endl;
这样,每个部分的算法和代码就展示出来了,下面贴出完整代码:
//炉石传说
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string Summon = "summon"; string Attack = "attack"; string End = "end";
int turn = 0;
class suicong
{
public:
int attackBility;
int live;
suicong(int atttack = 0, int life = 0) :attackBility(atttack), live(life) {
}
suicong() {}
};
vector<suicong> player[2];
int LuShi()
{
int N = 0, Num = 0, i = 0;
suicong play1(0, 30), play2(0, 30);
string command;
cin >> N;
vector<suicong>::iterator item;
while (N--)
{
cin >> command;
if (command == Summon) {
int attack = 0, life = 0, positon;
cin >> positon >> attack >> life;
if (positon > player[turn].size())
player[turn].push_back(suicong(attack, life));
else
player[turn].insert(player[turn].begin() + positon - 1, suicong(attack, life));
}
if (command == Attack) { int use = 0, to = 0; cin >> use >> to; if (!to) {//如果攻击对方玩家 if (turn) {//如果是二号玩家,攻击一号玩家 play1.live -= player[turn][use-1].attackBility; if (play1.live <= 0) break; } else {//如果是一号玩家,攻击二号玩家 play2.live -= player[turn][use-1].attackBility; if (play2.live <= 0) break; } } else//如果攻击对方随从 { if (!turn) {//一号攻击二号随从 player[turn+1][to - 1].live -= player[turn][use - 1].attackBility; player[turn][use - 1].live -= player[turn+1][to - 1].attackBility; if (player[turn+1][to - 1].live <= 0) player[turn+1].erase(player[turn+1].begin() + to - 1); } else//二号攻击一号随从 { player[turn - 1][to - 1].live -= player[turn][use - 1].attackBility; player[turn][use - 1].live -= player[turn-1][to - 1].attackBility; if (player[turn - 1][to - 1].live <= 0) player[turn - 1].erase(player[turn-1].begin() + to - 1); } } if (player[turn][use - 1].live <= 0) player[turn].erase(player[turn].begin() + use - 1); }
if (command == End) { if (turn) turn = 0; else turn = 1; }
}
if (!play2.live) cout << "1" << endl; if (!play1.live) cout << "-1" << endl; if (play1.live && play2.live) cout << "0" << endl; cout << play1.live << endl; cout << player[0].size(); for (item = player[0].begin(); item != player[0].end(); item++) { cout <<" "<< item->live; } cout << endl; cout << play2.live << endl; cout << player[1].size(); for (item = player[1].begin(); item != player[1].end(); item++) { cout << " " << item->live; } cout << endl;
return 0;
}
int main()
{
LuShi();
system("pause");
return 0;
}
以上就是全部代码,希望有朋友能指出我的错误,有什么还没有考虑的,让我没有通过那最后20%用例,麻烦了!
相关文章推荐
- CCF-炉石传说
- CCF_炉石传说
- CCF CSP 201609-3 炉石传说
- CCF 201609-3 炉石传说 (100分)
- CCF 201609-3 炉石传说
- CCF炉石传说
- ccf csp 炉石传说 Java
- CCF CSP 201609-3:炉石传说
- ccf 炉石传说
- CCF-炉石传说
- ccf炉石传说
- CCF CSP 201609-3 炉石传说(Java-100分)
- CCF CSP 编程题目和解答-----试题名称:炉石传说 -------201609-3
- CCF CSP 2016年9月第3题 炉石传说(模拟)
- 炉石传说 ccf 201609_3试题
- ccf-20160903--炉石传说
- CCF 201609-3 炉石传说 解题报告 模拟
- 4000 CCF-炉石传说
- CCF 201609-3 炉石传说
- 【CCF 炉石传说 模拟题】