KatInterview

Fewest Rounds to Slay a Scripted Boss With One Revive

Trees & GraphsAuto-graded · 20 test cases
HardAuto-gradedTrees & Graphsbfsstate-space-searchsimulation

Last 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_hp health points.
  • The hero begins with hero_max_hp health 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 round t (counting from t = 0) the boss hits for boss_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:

  1. Hero move. The hero picks exactly one move (damage, heal) from moves. The boss loses damage HP.
  2. Win check. If the boss's HP is now <= 0, the hero wins immediately, and the answer for that sequence is t + 1 rounds. The boss does not attack in this round, and the heal from this move is irrelevant.
  3. Heal. Otherwise the hero gains heal HP, capped at hero_max_hp.
  4. Boss attack. The boss hits the hero for boss_attacks[t % len(boss_attacks)].
  5. 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.

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 <= 150
  • 1 <= hero_max_hp <= 100
  • 1 <= revive_hp <= hero_max_hp
  • boss_attacks and moves are non-empty lists.
  • Every move has damage >= 0 and heal >= 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.