Fewest Rounds to Slay a Scripted Boss With One Revive
Trees & GraphsAuto-graded · 20 test casesLast updated
Find the minimum number of rounds to defeat a boss that attacks on a repeating script, using state-space BFS over boss HP, player HP, revive flag, and script position.
Fewest Rounds to Slay a Scripted Boss With One Revive
You are writing a planner for a turn-based duel between a hero and a boss. Each round, the hero acts first and then the boss, unless the boss has already fallen. Find the smallest number of rounds in which the hero can win, or report that winning is impossible.
Setup
- The boss begins with
boss_hphealth points. - The hero begins with
hero_max_hphealth points, which is also the most HP the hero can ever have. - The hero owns exactly one revive, which can be used at most once.
- The boss follows a fixed script,
boss_attacks, that repeats forever. In roundt(counting fromt = 0) the boss hits forboss_attacks[t % len(boss_attacks)]. - The hero has a list
moves. Each move is a pair(damage, heal).
Round Resolution
In every round, resolve things in exactly this order:
- Hero move. The hero picks exactly one move
(damage, heal)frommoves. The boss losesdamageHP. - Win check. If the boss's HP is now
<= 0, the hero wins immediately, and the answer for that sequence ist + 1rounds. The boss does not attack in this round, and the heal from this move is irrelevant. - Heal. Otherwise the hero gains
healHP, capped athero_max_hp. - Boss attack. The boss hits the hero for
boss_attacks[t % len(boss_attacks)]. - Revive check. If the hero's HP is now
<= 0(exactly zero counts):- If the revive has not been used, it is used now and the hero's HP is set to
revive_hp. - If the revive was already used, the hero is defeated and this line of play is invalid.
- If the revive has not been used, it is used now and the hero's HP is set to
The hero may choose any move in any round, and the same move can be chosen repeatedly. Consider every possible sequence of choices and return the smallest number of rounds that ends in a win. If no sequence wins, return -1.
Function
Implement fewest_rounds_to_win(boss_hp, hero_max_hp, revive_hp, boss_attacks, moves), where:
boss_hp: the boss's starting HP.hero_max_hp: the hero's starting and maximum HP.revive_hp: the HP the hero is restored to when the revive triggers.boss_attacks: the repeating list of boss damage values.moves: a list of(damage, heal)pairs.
It returns the minimum number of rounds needed to win, or -1 if the hero cannot win.
Constraints
1 <= boss_hp <= 1501 <= hero_max_hp <= 1001 <= revive_hp <= hero_max_hpboss_attacksandmovesare non-empty lists.- Every move has
damage >= 0andheal >= 0, and at least one of them is positive. - Boss attack values are non-negative integers.
- If either list were empty, the answer is
-1.
Examples
Example 1
Input: fewest_rounds_to_win(30, 20, 10, [15, 10], [[10, 0], [0, 8]])
Output: 3
Example 2
Input: fewest_rounds_to_win(50, 10, 5, [20], [[5, 0]])
Output: -1
Hints
Hint 1
Think of each situation as a node in a graph and each round as an edge. Since every edge costs one round, which graph algorithm gives the shortest path?
Hint 2
You do not need the round number in the state, only the position in the boss's script (round index modulo script length). What else must the state contain: boss HP, hero HP, and whether the revive is still available?
Hint 3
Follow the resolution order exactly: check for the win before healing or being attacked, cap the heal before applying the boss's hit, and treat HP of exactly 0 as a death. Deduplicate states when you enqueue them so the search terminates.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Model the duel as a shortest-path problem. A node is a snapshot of the fight:
(boss HP, hero HP, revive still available, position in boss script)
Each round is an edge: the hero picks a move, then the boss attacks. Every edge costs one round, so the graph is unweighted and breadth-first search finds the minimum number of rounds.
The round number does not need to be stored. It only matters through t % len(boss_attacks), which is the script position already in the state. Two histories that reach the same tuple have identical futures, so each tuple needs to be expanded only once.
Simulating one round
From a state with a chosen move (damage, heal):
- If
damage >= boss HP, the boss dies before acting, so returnrounds + 1. - Heal, capped at
hero_max_hp. - Subtract the boss's scripted hit.
- If HP is
<= 0: use the revive (set HP torevive_hp) if it is still available, otherwise discard this branch. - Enqueue the new state
(boss HP - damage, new HP, revive flag, next script index)if it has not been seen.
Why it is correct
- Sufficient state: all future transitions depend only on the four components, so merging equal states loses nothing.
- Optimality: BFS processes states in non-decreasing round count. The first state that has a winning move is at the smallest depth
d, sod + 1is the minimum. Returning while generating neighbors is safe because every neighbor of a depth-dstate is at depthd + 1, and any shallower win would have been found earlier. - Termination: every component is bounded, so the state space is finite, and the
seenset ensures each state is enqueued once. Pure-heal moves cannot cause infinite loops. If the queue empties, no win exists and the answer is-1.
Complexity
With B = boss_hp, P = hero_max_hp, L = len(boss_attacks), M = len(moves):
- States: at most
B * P * 2 * L. - Time:
O(B * P * L * M). - Space:
O(B * P * L).
Reference solution
from collections import deque
from typing import List, Tuple
def fewest_rounds_to_win(boss_hp: int, hero_max_hp: int, revive_hp: int,
boss_attacks: List[int],
moves: List[Tuple[int, int]]) -> int:
if boss_hp <= 0:
return 0
if not boss_attacks or not moves:
return -1
# State: (boss HP, hero HP, revive available 1/0, index in boss script)
start = (boss_hp, hero_max_hp, 1, 0)
queue = deque([(start, 0)])
seen = {start}
script_len = len(boss_attacks)
while queue:
(b_hp, h_hp, revive_left, idx), rounds = queue.popleft()
hit = boss_attacks[idx]
next_idx = (idx + 1) % script_len
for damage, heal in moves:
if damage >= b_hp:
return rounds + 1
new_hp = min(hero_max_hp, h_hp + heal) - hit
next_revive = revive_left
if new_hp <= 0:
if not revive_left:
continue
new_hp = revive_hp
next_revive = 0
state = (b_hp - damage, new_hp, next_revive, next_idx)
if state not in seen:
seen.add(state)
queue.append((state, rounds + 1))
return -1