算法刷题记录(Day 65)

炉石传说

原题链接
注意点:
1.战场的随从都是从1开始编号的
2.麻烦点在于随从的插入和删除需要进行大量的移动操作->.n的范围较小,可以直接来进行移动
3.当本方战场有 7 个随从时,不会再召唤新的随从->不会出现溢出的情况

#include<iostream>
using namespace std;
#define NMAX 8
int n;
int position, attack, health, attacker, defender;
int order = 0;
struct person {
	int attack, health;
}war[2][NMAX];
int anum[2];
void insert(int o) {
	for (int i = anum[o]; i >= position; i--) {
		swap(war[o][i], war[o][i + 1]);
	}
	war[o][position].attack = attack;
	war[o][position].health = health;
	anum[o]++;
}
void clear(int o, int p) {
	if (!p) return; //如果是hero死亡了,不要移动
	for (int i = p + 1; i <= anum[o]; i++) { //如果p=1,
		swap(war[o][i - 1], war[o][i]);
	}
	anum[o]--;
}
void fight(int o1,int o2) {
	war[o1][attacker].health -= war[o2][defender].attack;
	war[o2][defender].health -= war[o1][attacker].attack;
	if (war[o1][attacker].health <= 0) clear(o1,attacker);
	if (war[o2][defender].health <= 0) clear(o2, defender);
}
void output(int o) {
	cout << war[o][0].health << endl;
	cout << anum[o];
	for (int i = 1; i <= anum[o]; i++) cout << " " << war[o][i].health;
	cout << endl;
}
int main() {
	cin >> n;
	anum[0] = anum[1] = 0;
	war[0][0].attack = war[1][0].attack = 0;
	war[0][0].health = war[1][0].health = 30;
	while (n--) {
		string s;
		cin >> s;
		if (s == "summon") {
			cin >> position >> attack >> health;
			insert(order%2);
		}
		else if (s == "attack") {
			cin >> attacker >> defender;
			fight(order%2,(order+1)%2);
		}
		else {
			order++;
		}
		if (war[0][0].health > 30 || war[1][0].health > 30) {
			cout << "error" << endl;
		}
	}
	if (war[0][0].health <= 0) {
		cout << "-1" << endl;
	}
	else if (war[1][0].health <= 0) {
		cout << "1" << endl;
	}
	else cout << "0" << endl;
	output(0);
	output(1);

}

tip:
1.如果是hero死亡了,那么不要移动hero的位置
2.NMAX的值应该设置为8,而不应该是7,否则会产生溢出

换教室

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值