Play the Dragon
Time limit5sMemory limit512 MB
Given dragon and knight stats, find the fewest turns of attack, buff, cure, or debuff actions to defeat the knight, or report it is impossible.
- Level
Medium7 of 10
- Topics
- Brute force, Greedy, Simulation, Implementation
- Solved
- No attempts yet
Problem
You are a friendly dragon defending your lair from a greedy knight. You have health points and an attack power of , and the knight has health points and an attack power of . If your health drops to 0 or below at any point, you are knocked out and you lose immediately. If the knight's health drops to 0 or below at any point, the knight is knocked out and you win.
The battle runs in turns. On each turn you act first, and you choose exactly one of the following actions.
- Attack: reduce the opponent's health by your own attack power.
- Buff: increase your attack power by for the rest of the battle.
- Cure: set your health to .
- Debuff: decrease the opponent's attack power by for the rest of the battle. If this would make the opponent's attack power less than 0, it becomes 0 instead.
After your action, if the knight's health is greater than 0, the knight attacks, and then the turn ends. A turn in which you defeat the knight still counts as a turn, even though the knight does not get to act.
Buffs stack, so every buff adds another to your attack power. Debuffs stack the same way.
You want to defeat the knight as fast as possible, because you do not want to be late for roasting marshmallows with the villagers at tonight's festival. Find the minimum number of turns in which you can defeat the knight, or report that defeating the knight is impossible.
Input
The first line contains the number of test cases . Each of the next lines contains six integers , , , , , and , separated by spaces.
Limits
Output
For each test case, print one line in the form Case #x: y, where x is the test case number starting from 1 and y is the minimum number of turns needed to defeat the knight. If the knight cannot be defeated, print IMPOSSIBLE in place of y.
Hint
In the first test case you have 11 health and 5 attack, and the knight has 16 health and 5 attack. One optimal sequence of actions is:
- Turn 1: Attack, reducing the knight's health to 11. The knight then attacks and reduces your health to 6.
- Turn 2: Attack, reducing the knight's health to 6. The knight then attacks and reduces your health to 1.
- Turn 3: Cure, restoring your health to 11. The knight then attacks and reduces your health to 6. (If you attacked this turn instead, the knight's next attack would knock you out.)
- Turn 4: Attack, reducing the knight's health to 1. The knight then attacks and reduces your health to 1.
- Turn 5: Attack, reducing the knight's health to -4. You win immediately and the knight does not attack again.
In the second test case one optimal sequence of actions is:
- Turn 1: Buff, raising your attack power to 3. The knight then attacks and reduces your health to 1.
- Turn 2: Attack, reducing the knight's health to 0. You win immediately and the knight does not attack again.
In the third test case the knight needs only two attacks to knock you out, and you cannot deal enough damage fast enough. Using Cure after every attack keeps the battle going forever, but the knight can never be defeated.
In the fourth test case one optimal sequence of actions is Attack, Debuff, Buff, Attack, Attack.