Dungeon Game

Introduction

 
In this article, we will sove the leetcode problem #174, Dungeon Game. The problem statement goes like this:
 
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
 
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).
 
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
 
Write a function to determine the knight's minimum initial health so that he is able to rescue the princess. For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.
  1. static void Main(string[] args)  
  2. {  
  3.     var result = calculateMinimumHP(new int[][]  
  4.     {  
  5.         new int[]{ -2, -3, 3 },  
  6.         new int[]{ -5, -10, 1 },  
  7.         new int[]{ 10, 30, -5 }  
  8.     });  
  9.     //result = 7  
  10. }  

Solution

 
The best approach for problems like this is dynamic programming, in which we will find the value of each cell depending on the previously calculated cell value. So the algorithm basically goes like,
  1. We will take a empty 2d array of same dimensions as dungeon, and fill the last cell, i.e. dungeon[n-1][n-1].
  2. Then we will keep finding the health of the Knight(K) by traversing over the empty dungeon.
  3. First we will fill the last column(DOWN<--DOWN) and then, fill the last row(RIGHT<--RIGHT).
  4. And finally, we fill up the rest of the empty positions to get on to the 1st cell, i.e. health[0, 0].
Here is the C# code for the approach,
  1. static int calculateMinimumHP(int[][] dungeon)  
  2. {  
  3.     if (dungeon == null || dungeon.Length == 0)  
  4.     {  
  5.         return 0;  
  6.     }  
  7.   
  8.     int height = dungeon.Length;  
  9.     int width = dungeon[0].Length;  
  10.     int[,] health = new int[height, width];  
  11.     health[height - 1, width - 1] =  
  12.             (dungeon[height - 1][width - 1] > 0) ? 1 : 1 - dungeon[height - 1][width - 1];  
  13.   
  14.     //fill the last column  
  15.     for (int i = height - 2; i >= 0; i--)  
  16.     {  
  17.         int temp = health[i + 1, width - 1] - dungeon[i][width - 1];  
  18.         health[i, width - 1] = Math.Max(1, temp);  
  19.     }  
  20.   
  21.     //fill the last row  
  22.     for (int j = width - 2; j >= 0; j--)  
  23.     {  
  24.         int temp = health[height - 1, j + 1] - dungeon[height - 1][j];  
  25.         health[height - 1, j] = Math.Max(temp, 1);  
  26.     }  
  27.   
  28.     for (int i = height - 2; i >= 0; i--)  
  29.     {  
  30.         for (int j = width - 2; j >= 0; j--)  
  31.         {  
  32.             int down = Math.Max(1, health[i + 1, j] - dungeon[i][j]);  
  33.             int right = Math.Max(1, health[i, j + 1] - dungeon[i][j]);  
  34.             health[i, j] = Math.Min(down, right);  
  35.         }  
  36.     }  
  37.       
  38.     return health[0, 0];  
  39. }