Money the capybara has become fascinated by turn-based strategy games. He wants to build a tiny battle simulator to study how cavalry, infantry, and archers behave. Instead of writing one giant block of conditional statements, Money dec
ides to model the units as objects with shared behaviors and different class bonuses.
Your task is to complete the simulator and print every soldier's HP and position after exactly T seconds.

The battlefield has N rows and M columns. All coordinates are 1-based. There are P1 red soldiers and P2 blue soldiers. There are no obstacles. Any number of soldiers, including enemies, may occupy the same cell.
Every soldier has an ID, team, HP, movement speed, attack damage, attack range, armor, and position (row, col). Three subclasses share the same basic behaviors:
C): actual speed = input speed multiplied by C_bonus.I): initial HP = input HP multiplied by I_bonus.A): actual attack range = input range multiplied by A_bonus.All three bonuses are input integers and apply equally to both teams. A unit's bonus is applied exactly once when the unit is created. All other attributes remain unchanged. Armor never receives a class bonus.
The distance between positions (r1,c1) and (r2,c2) is Manhattan distance:
distance = abs(r1 - r2) + abs(c1 - c2)
At the start of each action, a living soldier selects the closest living enemy. If multiple enemies are equally close, choose the one with the smallest global ID. The chosen target stays fixed during this action. If no living enemy exists, the soldier does nothing.
Each of the T seconds consists of one Red Phase followed by one Blue Phase. Within each team, soldiers act in their original input order (increasing ID). Changes to HP and position take effect immediately. A soldier that dies before its turn is skipped.
Let D be the current distance to the chosen enemy, R the moving soldier's actual attack range, and S its actual movement speed.
If D <= R, the soldier does not move and attacks once. Otherwise it moves toward the target by exactly:
steps = min(S, D - R)
Move toward the target's row first until either the row matches or all movement steps are spent; then move toward the target's column with any remaining steps. This makes movement deterministic. The soldier stops as soon as it enters attack range, even if unused movement speed remains.
If it reaches attack range after moving, it attacks the same target immediately. A soldier can attack at most once per action.
Let K be the global armor coefficient, Damage the attacker's damage, and Armor the defender's armor. The integer damage taken is:
DamageTaken = max(1, (K * Damage) // (K + Armor))
The division uses integer floor, and every successful attack deals at least 1 point of damage. Update HP as:
HP = max(0, HP - DamageTaken)
When HP reaches zero, the soldier dies immediately. Dead soldiers neither act nor become targets, but their final position is retained and must still be output. If one team is eliminated, survivors do nothing for the remaining seconds.
Implement the shared behavior in a Soldier base class, override apply_bonus in Cavalry, Infantry, and Archer, and manage the turn order in Battlefield. The starter code below includes the required attributes and method signatures; complete the TODO sections without changing the provided input/output format.
import sys
class Soldier:
def __init__(self, soldier_id, team, hp, speed, damage,
attack_range, armor, row, col):
# All soldiers share these attributes.
self.id = soldier_id
self.team = team
self.hp = hp
self.speed = speed
self.damage = damage
self.attack_range = attack_range
self.armor = armor
self.row = row
self.col = col
def is_alive(self):
# TODO: Return whether this soldier is alive.
pass
def distance_to(self, other):
# TODO: Return Manhattan distance to other.
pass
def find_target(self, enemies):
# TODO: Choose closest living enemy; ties -> smaller ID.
pass
def take_damage(self, raw_damage, k):
# TODO: Armor reduction, floor, minimum 1; HP cannot go below 0.
pass
def move_toward(self, target):
# TODO: Move row first, then column; stop at attack range.
pass
def attack(self, target, k):
# TODO: Attack target once if target is alive and in range.
pass
def act(self, enemies, k):
# TODO: Pick target, then attack or move (and attack if in range).
pass
class Cavalry(Soldier):
def apply_bonus(self, bonus):
# TODO: Multiply movement speed by bonus.
pass
class Infantry(Soldier):
def apply_bonus(self, bonus):
# TODO: Multiply initial HP by bonus.
pass
class Archer(Soldier):
def apply_bonus(self, bonus):
# TODO: Multiply attack range by bonus.
pass
class Battlefield:
def __init__(self, red, blue, k):
self.red = red
self.blue = blue
self.k = k
def run_phase(self, allies, enemies):
# TODO: Each living ally acts in input order.
pass
def simulate(self, t):
# TODO: Run T seconds; Red phase before Blue phase.
pass
def results(self):
# Provided output helper; do not change the format.
return "\n".join(
f"{unit.hp} {unit.row} {unit.col}"
for unit in self.red + self.blue
)
def read_soldier(soldier_id, team, bonuses):
parts = sys.stdin.buffer.readline().split()
kind = parts[0].decode()
hp, speed, damage, attack_range, armor, row, col = map(int, parts[1:])
classes = {'C': Cavalry, 'I': Infantry, 'A': Archer}
soldier = classes[kind](soldier_id, team, hp, speed, damage,
attack_range, armor, row, col)
soldier.apply_bonus(bonuses[kind])
return soldier
def main():
n, m, p1, p2, t = map(int, sys.stdin.buffer.readline().split())
k, c_bonus, i_bonus, a_bonus = map(int, sys.stdin.buffer.readline().split())
bonuses = {'C': c_bonus, 'I': i_bonus, 'A': a_bonus}
red = [read_soldier(i, 'R', bonuses) for i in range(1, p1 + 1)]
blue = [read_soldier(p1 + i, 'B', bonuses) for i in range(1, p2 + 1)]
battlefield = Battlefield(red, blue, k)
battlefield.simulate(t)
sys.stdout.write(battlefield.results() + '\n')
if __name__ == '__main__':
main()
1 <= N, M <= 100001 <= P1, P2 <= 301 <= T <= 3001 <= HP, Speed, Damage, Range <= 100000 <= Armor <= 10001 <= K <= 10001 <= C_bonus, I_bonus, A_bonus <= 51 <= Row <= N, 1 <= Column <= MThe first sample has two red soldiers and one blue soldier. After applying class bonuses, the red cavalry has speed 2, the red infantry has 20 initial HP, and the blue archer has range 4.
During second 1, the cavalry moves from (1,1) to (1,3), the infantry moves from (4,7) to (3,7), and the blue archer attacks the closer cavalry for 6 damage.
During second 2, the cavalry moves from (1,3) to (1,4) and attacks the archer. The archer's armor reduces 12 raw damage to (100 * 12) // (100 + 50) = 8. The infantry moves to (2,7), and the archer attacks the cavalry again for 6 damage. Thus the final HP values are 8, 20, and 17.
The first line contains five integers:
N M P1 P2 T
N and M are battlefield dimensions, P1 and P2 the red and blue soldier counts, and T the number of seconds.
The second line contains:
K C_bonus I_bonus A_bonus
K is the global armor coefficient. The other three values are the class multipliers for cavalry speed, infantry initial HP, and archer attack range.
The next P1 lines describe red soldiers. The following P2 lines describe blue soldiers. Each line contains:
Type HP Speed Damage Range Armor Row Column
Type is C, I, or A. All attributes on these lines are base values before class bonuses. Global IDs are assigned by input order, starting from 1 (all red soldiers appear first).
4 7 2 1 2
100 2 2 2
C 20 1 12 1 0 1 1
I 10 1 8 1 0 4 7
A 25 1 6 2 50 1 5
After exactly T seconds, print P1 + P2 lines in the original input order.
Each line contains:
HP Row Column
HP is the remaining HP (0 for a dead soldier). A dead soldier keeps its last position. All positions are 1-based.
8 1 4
20 2 7
17 1 5