csp simulation question-stones legend

Posted by BrandonRoy on Sat, 13 Jun 2020 03:35:26 +0200

subject

Question number: 201609-3
Test Name: Stone Legend
Time limit: 1.0s
Memory limit: 256.0MB
Description of the problem:

Hearthstone: Heroes of Warcraft is an exchange card game developed by Blizzard Entertainment (pictured below).The game is played on a battle board in which two players take turns. The simplified rules of the Stone Legend game used in this topic are as follows:

*Players control some characters, each with their own life and attack power.When the life value is less than or equal to zero, the character dies.The roles are divided into heroes and followers.
*Players each control a hero. At the beginning of the game, the hero's life value is 30 and his attack power is 0.When the hero dies, the game ends and the one who is not dead wins.
*Players can summon followers during the game.There are seven spaces on each side of the board that can be used to place followers, one word from left to right, called battlefields.When the follower dies, it will be removed from the battlefield.
*When the game starts, two players take turns to operate, and a continuous set of operations for each player is called a round.
*Current player can perform zero or more of the following actions in each round:
1) Call Followers: Players call a follower into the battlefield with a specified life value and attack power.
2) Following Attack: Players control one of their followers attacking an opponent's heroes or one of their followers.
3) End Round: The player declares that his current round is over and the game will enter his opponent's round.The operation must be the last operation in a round.
*When followers attack, both the attacker and the attacked cause the same damage to each other as their own attack.The life value of the injured character decreases, equating to the number of injuries suffered.For example, the life value of the follower X is HX, the attack force is AX, the life value of the follower Y is HY, and the attack force is AY. If the follower X follows Y, the life value of the follower X changes to HX - AY after the attack occurs, and the life value of the follower Y changes to HY - AX.After an attack occurs, a character's life value can be negative.
This topic will give you a process of playing a game that requires a program to simulate the process and output the final situation.

Input Format

The first line of input is an integer n representing the number of operations.Next n lines, each describing an operation in the following format:
  < action > < arg1 > < arg2 > ...
Where < action > denotes the type of operation and is a string of three types: summon for summon follower, attack for follower attack, and end for end round.The specific formats for these three operations are as follows:
* summon < position > < attack > < health >: The current player summons a follower with < Health > life and < attack > attack in position.An integer from 1 to 7 indicates where the summoned follower appears on the battlefield. The follower and the follower on the right will move one bit in turn to the right.
* attack < attacker > < defender >: current player's role < attacker > attacker's role < defender >.< attacker > is an integer from 1 to 7, indicating the follower number of the attacking party, < defender > is an integer from 0 to 7, indicating the character of the attacked party, 0 being the hero of the attacking party, and 1 to 7 being the follower number of the attacking party.
* end: The current player ends this round.
Note: The number of followers changes as the game progresses. When a follower is summoned, the player specifies the location where the follower is called into the battlefield. At this time, all follower numbers on the right and the original location will be increased by 1.When a follower dies, all the follower numbers on its right are reduced by 1.At any moment, the followers on the battlefield are numbered continuously from the beginning.

Output Format

The output is a total of 5 lines.
Line 1 contains an integer indicating the outcome of the game after n operations (hereinafter referred to as T-Moment), 1 indicating the first player wins, 1 indicating the second player wins, 0 indicating that the game is not over and no one wins.
Line 2 contains an integer representing the life value of the hero of the first-hand player at T.
Line 3 contains several integers. The first integer, p, represents the number of followers who survive on the battlefield at the time of T, followed by P integers, which represent the life values of these followers at the time of T (in left-to-right order).
Rows 4 and 5 are similar to rows 2 and 3 except that the player changes from a first-hand player to a second-hand player.

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

sample output

0
30
1 2
30
1 2

Sample Description

As a sample input, the line-by-line explanations starting at line 2 are as follows:
1. The first player summons a follower A with a life value of 6 and an attack power of 3 at position 1, which is the only follower on the local battlefield.
2. The first-hand player summons a follower B with a life value of 2 and an attack power of 4 at position 2, appearing on the right side of follower A.
3. End of first-hand player round.
4. The backhand player summons a follower C with a life value of 5 and an attack power of 4 at position 1, which is the only follower on the local battlefield.
5. The backhand player summons a follower D with life value 1 and attack power 2 at position 1, appearing on the left side of follower C.
6. Following D attacks Following B, and both sides die.
7. End of backhand round.
8. Following A Attack Following C reduces both life values to 2.

Analysis

1. In general, do simulation exercises, start with the main function, and write down what should be input. At this time, you will think about how to record each role (life, attack), just record with sturct type:

struct Role {
	int H;			//Value of Life
	int A;			//Aggressivity
	Role(){H=30,A=0};//The default hero life value is 30 and attack power is 0
	Role(int h, int a) {H=h;A=a};//Given follower's vitality and attack
};

2. Once you have considered how to record, there are three operations, one of which we can write the corresponding operation function:
1) Call follower: This function gives position, life, attack value, current player, so that this follower can be added to the current player

void SummonRetinue(int position,int attack,int health,vector<Role> &player) {//Give location, life, attack power and current player (A\B), call followers (that is, add followers to a player)
	Role retinue = { health,attack};
	auto i = player.begin();
	i += position;
	player.insert(i, retinue);//Add a follower to this location
}

2. Attack: Give the current player and the current player's attacker position; the opposing player and the opposing player's defender position; and then just subtract the other player's attack value from the life value of both players, but what you need to do is if one of the players dies (you need to delete the follower), and if the attacked hero dies, the game ends.

void Attack(int attacker,int defender,vector<Role> &playerAttack,vector<Role> &playerDefend) {//
	playerAttack[attacker].H -= playerDefend[defender].A;
	playerDefend[defender].H -= playerAttack[attacker].A;
	if (playerAttack[attacker].H <= 0)//The attacker's follower died
		ClearDeath(attacker, playerAttack);
	if (playerDefend[defender].H <= 0) {//The follower or hero of the attacked party died
		if (defender == 0)//If the hero dies, the game is over
			return;
		ClearDeath(defender, playerDefend);
	}
}

3) end of Round: At this point, you only need to exchange the current player and the other player and record the two states with flag.

Internal os, this topic and the previous directory manager's topic, have different ideas, but this is slightly simpler...

Code

#include<bits/stdc++.h>
using namespace std;
struct Role {
	int H;			//Value of Life
	int A;			//Aggressivity
	Role(){H=30,A=0};//The default hero life value is 30 and attack power is 0
	Role(int h, int a) {H=h;A=a};//Given follower's vitality and attack
};
//Remove the dead followers from the battlefield
void ClearDeath(int position, vector<Role> &player){
	auto i = player.begin();
	i += position;
	player.erase(i);
}
//playerAttack Attacks playerDefend
void Attack(int attacker,int defender,vector<Role> &playerAttack,vector<Role> &playerDefend) {
	playerAttack[attacker].H -= playerDefend[defender].A;
	playerDefend[defender].H -= playerAttack[attacker].A;
	if (playerAttack[attacker].H <= 0)//The attacker's follower died
		ClearDeath(attacker, playerAttack);
	if (playerDefend[defender].H <= 0) {//The follower or hero of the attacked party died
		if (defender == 0)//If the hero dies, the game is over
			return;
		ClearDeath(defender, playerDefend);
	}
}
//Call followers
void SummonRetinue(int position,int attack,int health,vector<Role> &player) {//Give location, life, attack power and current player (A\B), call followers (that is, add followers to a player)
	Role retinue = { health,attack};
	auto i = player.begin();
	i += position;
	player.insert(i, retinue);//Add a follower to this location
}
//Output hero's life value and follow-up
void Output(vector<Role> &player) {
	cout << player[0].H << endl;
	int number = player.size() - 1;
	cout << number;
	for (int i = 0; i < number; i++)
		cout << " " << player[i + 1].H;
	cout << endl;
}
int main()
{
	int n;
	string str;
	Role hero;
	int flag = 1;//Mark whether the current round is first-hand or second-hand, 1 is first-hand and 0 is second-hand
	vector<Role> playerARole, playerBRole;//The roles are first-hand and second-hand players, with the 0th element being a hero and the follower being the follower
	playerARole.push_back(hero);
	playerBRole.push_back(hero);
	cin >> n;
	while (n--) {
		cin >> str;
		if (str == "summon") {
			int position, attack, health;
			cin >> position >> attack >> health;
			if(flag)//The current round is the first player
				SummonRetinue(position, attack, health, playerARole);
			else
				SummonRetinue(position, attack, health, playerBRole);
		}
		else if (str == "attack") {
			int attacker, defender;
			cin >> attacker >> defender;
			if (flag)//The current round is when the first player attacks the second player
				Attack(attacker, defender, playerARole,playerBRole);
			else
				Attack(attacker, defender, playerBRole,playerARole);
		}
		else //Switch Players
			flag = !flag;
	}
	if (playerARole[0].H <= 0)
		cout << -1 << endl;
	else if (playerBRole[0].H <= 0)
		cout << 1 << endl;
	else
		cout << 0 << endl;
	Output(playerARole);
	Output(playerBRole);
}

Topics: less