Imagine a group of n players that are located in different floors. The i-th player (for…
- User
Imagine a group of n players that are located in different floors. The i-th player (for each i such that 1 ≤ i ≤ n, where i is an integer) is characterized by the following: • pi, a unique starting floor level • hi, a starting HP (Health Points) value • di, a direction of movement, either U indicating that the i-th player is going upward or D indicatingthat the i-th player is going downward All players move simultaneously at the same speed in their respective directions. When two playersmeet, they engage in a battle. The player with the lower health is eliminated instantaneously, while the other continue to move in the same direction at the same speed but with a reduced HP by 1. In acase where two players have identical HP, they are both eliminated simultaneously after the battle.Your task is to determine the final HP of the surviving players. 1.2 Input The first line of input contains an integer n, representing the number of players. The next n lines follow. The i-th line contains three values: pi , hi , di - an integer representing the starting floor level for the i-th player, an integer representing the HP value for the i-th player, and a character representing the direction of movement for the i-th player. 1.3 Output For each surviving player, output their remaining HP on a separate line, in the order of their input。write a fast program to solve this question by Python.使用二叉树
- Assistant
Here is a Python program that solves the given problem using a binary tree approach:
class Node: def __init__(self, floor, hp): self.floor = floor self.hp = hp self.left = None self.right = None def add_player(root, floor, hp, direction): if root is None: return Node(floor, hp) if root.floor == floor: if root.hp < hp: root.hp = hp elif root.hp == hp: root.hp -= 1 return root if (direction == 'U' and root.floor < floor) or (direction == 'D' and root.floor > floor): root.right = add_player(root.right, floor, hp, direction) else: root.left = add_player(root.left, floor, hp, direction) return root def print_final_hp(root): if root is None: return print_final_hp(root.left) if root.hp > 0: print(root.hp) print_final_hp(root.right) # Read number of players n = int(input()) root = None # Read player information and add them to the binary tree for _ in range(n): floor, hp, direction = input().split() floor = int(floor) hp = int(hp) root = add_player(root, floor, hp, direction) # Print the final HP of surviving players print_final_hp(root) - User
change to a right and faster way
- Assistant
Here's an updated version of the program that uses a dictionary to store the player information, making it faster and more efficient:
def determine_final_hp(players): player_hp = {} for player in players: floor, hp, direction = player if floor not in player_hp: player_hp[floor] = hp elif player_hp[floor] < hp: player_hp[floor] = hp elif player_hp[floor] == hp: player_hp[floor] -= 1 final_hp = [hp for hp in player_hp.values() if hp > 0] final_hp.sort() return final_hp # Read number of players n = int(input()) players = [] # Read player information and add them to a list for _ in range(n): floor, hp, direction = input().split() players.append((int(floor), int(hp), direction)) # Determine the final HP of surviving players final_hp = determine_final_hp(players) # Print the final HP of surviving players for hp in final_hp: print(hp)