LeetCode – Dungeon Game (Java)

Example:

-2 (K)	-3	3
-5	-10	1
10	30	-5 (P)

Java Solution

This problem can be solved by using dynamic programming. We maintain a 2-D table. h[i][j] is the minimum health value before he enters (i,j). h[0][0] is the value of the answer. The left part is filling in numbers to the table.

 public int calculateMinimumHP(int[][] dungeon) { int m = dungeon.length; int n = dungeon[0].length;   //init dp table int[][] h = new int[m][n];   h[m - 1][n - 1] = Math.max(1 - dungeon[m - 1][n - 1], 1);   //init last row for (int i = m - 2; i >= 0; i--) { h[i][n - 1] = Math.max(h[i + 1][n - 1] - dungeon[i][n - 1], 1); }   //init last column for (int j = n - 2; j >= 0; j--) { h[m - 1][j] = Math.max(h[m - 1][j + 1] - dungeon[m - 1][j], 1); }   //calculate dp table for (int i = m - 2; i >= 0; i--) { for (int j = n - 2; j >= 0; j--) { int down = Math.max(h[i + 1][j] - dungeon[i][j], 1); int right = Math.max(h[i][j + 1] - dungeon[i][j], 1); h[i][j] = Math.min(right, down); } }   return h[0][0]; }
Category >> Algorithms >> Interview
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code>
String foo = "bar";
</code></pre>
• Alik Elzin

Does the question mean to find the minimal health so he could find a route or minimal health so he could go on all routes?

• Alik Elzin

https://leetcode.com/problems/dungeon-game/

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.

@stellary

• Ing

• omnomnipotent

Thank you Stellari – you would think that this information would be presented up front. To Programcreek – don’t assume that everyone knows what this type of problem is; a simple explanation would do and save trouble for everyone.

• Candis