csp20160903-炉石传说

炉石传说

在这里插入图片描述在这里插入图片描述在这里插入图片描述

Examples

在这里插入图片描述

题目分析

  1. 其实这道题并不复杂
  2. 设置两个角色结构体数组,分别表示玩家1与玩家二的随从(包括英雄)
  3. 设置两个函数summon和attack
void summon(int p,int a,int h) {
	role.attack = a;
	role.health = h;
	copy(f[turn] + p, f[turn] + tot[turn] + 1, f[turn] + p + 1);
	f[turn][p] = role;
	tot[turn]++;
}
void attack_p(int at) {//攻击英雄
	f[1 - turn][0].health -= f[turn][at].attack;
}
void attack_f(int at,int de) {//攻击随从
	f[turn][at].health -= f[1 - turn][de].attack;
	f[1 - turn][de].health -= f[turn][at].attack;
	if (f[turn][at].health <= 0)//会出现死亡情况
	{
		copy(f[turn] + at + 1, f[turn] + tot[turn] + 1, f[turn] + at);
		tot[turn]--;
	}
	if (f[1 - turn][de].health <= 0)
	{
		copy(f[1 - turn] + de + 1, f[1 - turn] + tot[1 - turn] + 1, f[1 - turn] + de);
		tot[1 - turn]--;
	}
}

完整代码

#include<iostream>
#include<vector>
using namespace std;
int n, tot[2], turn = 0;
struct Role
{
	int health, attack;
}role;
Role f[2][8];
void summon(int p,int a,int h) {
	role.attack = a;
	role.health = h;
	copy(f[turn] + p, f[turn] + tot[turn] + 1, f[turn] + p + 1);
	f[turn][p] = role;
	tot[turn]++;
}
void attack_p(int at) {
	f[1 - turn][0].health -= f[turn][at].attack;
}
void attack_f(int at,int de) {
	f[turn][at].health -= f[1 - turn][de].attack;
	f[1 - turn][de].health -= f[turn][at].attack;
	if (f[turn][at].health <= 0)
	{
		copy(f[turn] + at + 1, f[turn] + tot[turn] + 1, f[turn] + at);
		tot[turn]--;
	}
	if (f[1 - turn][de].health <= 0)
	{
		copy(f[1 - turn] + de + 1, f[1 - turn] + tot[1 - turn] + 1, f[1 - turn] + de);
		tot[1 - turn]--;
	}
}
void print(int num) {
	cout << f[num][0].health << endl;
	cout << tot[num];
	for (int i = 1; i <= tot[num]; i++)
		cout << " " << f[num][i].health;
	cout << endl;
}
int main() 
{
	cin >> n;
	f[0][0].health = 30, f[0][0].attack = 0;
	f[1][0].health = 30, f[0][0].attack = 0;
	string ope;
	int arg1, arg2, arg3;
	int res = 0;
	while (n--)
	{
		cin >> ope;
		if (ope == "summon") {
			cin >> arg1 >> arg2 >> arg3;
			summon(arg1, arg2, arg3);
		}
		else if (ope == "attack") {
			cin >> arg1 >> arg2;
			if (arg2 == 0)
				attack_p(arg1);
			else
				attack_f(arg1, arg2);
		}
		else
			turn = 1 - turn;
		if (f[0][0].health <= 0)
		{
			res = -1;
			break;
		}
		if (f[1][0].health <= 0)
		{
			res = 1;
			break;
		}
	}
	cout << res << endl;
	print(0);
	print(1);
	return 0;
}

note

要多读题,多理解题意,在题目中会设置一些坑,比如这道题中游戏结束的意思是说,应该输出结果并返回,可能没有到约定的命令数,但一方死亡就不会有命令了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值