Dungeon Game - Problem

Imagine a brave knight on a heroic quest to rescue a princess trapped in a dangerous dungeon! 🏰⚔️

The dungeon is represented as an m x n grid where each room contains either:

  • Demons (negative values) - damage the knight's health
  • Magic orbs (positive values) - restore the knight's health
  • Empty rooms (0) - no effect on health

The knight starts at the top-left corner [0,0] and must reach the princess at the bottom-right corner [m-1,n-1]. The knight can only move right or down at each step.

Critical Rule: If the knight's health drops to 0 or below at any point, he dies immediately! 💀

Your task is to determine the minimum initial health the knight needs to successfully rescue the princess, ensuring his health never drops to 0 or below during the journey.

Input & Output

example_1.py — Classic Dungeon
$ Input: [[-4,5,2],[1,-4,0]]
Output: 5
💡 Note: The optimal path is Right→Down→Right. Starting with 5 HP: at (-4) we have 1 HP, at (5) we have 6 HP, at (-4) we have 2 HP, at (0) we still have 2 HP. Any less initial health would cause death at the first cell.
example_2.py — Single Positive Cell
$ Input: [[1,2],[3,4]]
Output: 1
💡 Note: All rooms contain positive values or are empty, so we only need the minimum possible health of 1 to start with.
example_3.py — Single Cell Dungeon
$ Input: [[-5]]
Output: 6
💡 Note: The knight faces a -5 damage in the only room. He needs at least 6 initial health to survive: 6 + (-5) = 1 HP remaining.

Constraints

  • 1 ≤ m, n ≤ 200
  • -1000 ≤ dungeon[i][j] ≤ 1000
  • The knight can only move right or down
  • Health must remain ≥ 1 at all times

Visualization

Tap to expand
Dungeon Game - Dynamic Programming Solution INPUT Knight Start: [0,0] Goal: [1,2] Dungeon Grid (2x3) -4 +5 +2 +1 -4 0 Demon (damage) Magic orb (heal) Empty room [[-4,5,2],[1,-4,0]] ALGORITHM STEPS 1 Start from End Work backwards from [m-1,n-1] 2 DP Formula dp[i][j] = max(1, min(right,down)-val) 3 Fill DP Table Bottom-up, right-to-left 4 Answer at [0,0] Minimum health needed DP Table (min health needed) 7 1 1 5 5 1 Start Ans dp[0][0] = max(1, 1-(-4)) = 7 min(dp[0][1], dp[1][0]) = min(1,5) 7 - 4 = 3, then +5-4+0 = 4 FINAL RESULT Minimum Health 7 Optimal Path -4 +5 +2 +1 -4 0 Health Journey: Start: 7 [0,0]: 7 + (-4) = 3 [0,1]: 3 + 5 = 8 [1,1]: 8 + (-4) = 4 [1,2]: 4 + 0 = 4 OK - Never below 1 Key Insight: Work BACKWARDS from princess to knight! At each cell, calculate minimum health needed to survive the rest of the journey. This avoids the complexity of tracking all possible forward paths. Formula: dp[i][j] = max(1, min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j]). Health must always be at least 1. TutorialsPoint - Dungeon Game | Optimal Solution (Backward DP)
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
73.9K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen