201609-3 炉石传说
Description
Sample
Input:
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
Output:
0
30
1 2
30
1 2
Idea
题意:两方英雄各自可以拥有最多7个随从,轮流进行回合,在自己的回合里可以进行3种操作零次或多次,求出n次操作后两方的状态。
- 初始化角色类(英雄和随从共用一个结构)
记录角色编号,生命值、攻击值,英雄还记录随从的数量
player0代表先手英雄,player1代表后手英雄,f0记录先手英雄的随从,f1记录后手英雄的随从 - 进行n次操作,t=0代表先手玩家的回合,t=1代表后手玩家的回合
根据操作种类,分别进行以下3种操作:
1. 召唤随从:根据t判断是先手玩家还是后手玩家的回合,将输入的随从加入到f0或f1,如果该随从的编号小于目前玩家随从的总数,则需要将该编号及以后的随从向后移动一位并更新编号
2. 随从攻击:根据t判断是攻击方与受攻击方,如果己方随从攻击对方的随从,两个随从都要扣相应的血,然后判断这两个随从是否死亡,如果死亡则把随从从英雄的随从里移除,并把其之后的随从向前移动一位并更新编号。如果攻击对方英雄,则对方英雄扣血即可。每次攻击结束后判断两方英雄有没有阵亡,阵亡则不再进行以后的操作,输出结果。
3. 回合结束:更新t,变换回合 - 判断两方英雄的生死,输出需要的结果
Summary
这道模拟题不难,分别实现三种操作即可,用t判断当前是哪方的回合,t不同三种操作的对象也就不同。注意每次攻击后判断两方英雄有没有死亡的,有则直接出结果。
在把随从向前移或向后移的操作里直接用copy可能会出问题,向前移就从当前位置往后循环,向后移就从后往当前位置循环。
Codes
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;
string action[3] = { "summon","attack","end" };
struct role {
int pos;
int health;
int attack = 0;
int num = 0;
role(){}
role(int p, int h, int a) {
pos = p, health = h, attack = a;
}
};
role player0(0, 30, 0), player1(0, 30, 0);
role f0[8], f1[8];
int t = 0,ans;
void summon(role temp);
void attacking(int x,int y);
int main()
{
cin >> n;
for (int ope = 0; ope < n; ope++)
{
string act;
cin >> act;
if (act == action[0]) {
//summon
role temp;
cin >> temp.pos >> temp.attack >> temp.health;
summon(temp);
}
else if (act == action[1]) {
//attack
int attacker, defender;
cin >> attacker >> defender;
attacking(attacker, defender);
if (player0.health <= 0)break;
if (player1.health <= 0)break;
}
else if (act == action[2]) {
//end
t = (t + 1) % 2;
continue;
}
}
if (player0.health <= 0)ans = -1;
if (player1.health <= 0)ans = 1;
if (player0.health > 0 && player1.health > 0)ans = 0;
printf("%d\n", ans);
printf("%d\n", player0.health);
printf("%d", player0.num);
for (int i = 1; i <= player0.num; i++)
printf(" %d", f0[i].health);
printf("\n");
printf("%d\n", player1.health);
printf("%d", player1.num);
for (int i = 1; i <= player1.num; i++)
printf(" %d", f1[i].health);
printf("\n");
}
void summon(role flr) {
if (t == 0) {
if (player0.num < flr.pos) {
player0.num++;
f0[player0.num] = flr;
}
else {
for (int i = player0.num + 1; i > flr.pos; i--) {
f0[i - 1].pos++;
f0[i] = f0[i - 1];
}
f0[flr.pos] = flr;
player0.num++;
}
}
else if (t == 1) {
if (player1.num < flr.pos) {
player1.num++;
f1[player1.num] = flr;
}
else {
for (int i = player1.num + 1; i > flr.pos; i--) {
f1[i - 1].pos++;
f1[i] = f1[i - 1];
}
f1[flr.pos] = flr;
player1.num++;
}
}
}
void attacking(int x, int y) {
if (t == 0) {
if (y != 0) {
f0[x].health -= f1[y].attack;
f1[y].health -= f0[x].attack;
if (f0[x].health <= 0) {
for (int i = x; i < player0.num; i++) {
f0[i + 1].pos--;
f0[i] = f0[i + 1];
}
player0.num--;
}
if (f1[y].health <= 0) {
for (int i = y; i < player1.num; i++) {
f1[i + 1].pos--;
f1[i] = f1[i + 1];
}
player1.num--;
}
}
else {
player1.health-= f0[x].attack;
}
}
else if (t == 1) {
if (y != 0) {
f1[x].health -= f0[y].attack;
f0[y].health -= f1[x].attack;
if (f0[y].health <= 0) {
for (int i = y; i < player0.num; i++) {
f0[i + 1].pos--;
f0[i] = f0[i + 1];
}
player0.num--;
}
if (f1[x].health <= 0) {
for (int i = x; i < player1.num; i++) {
f1[i + 1].pos--;
f1[i] = f1[i + 1];
}
player1.num--;
}
}
else {
player0.health -= f1[x].attack;
}
}
}