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];
}

8 thoughts on “LeetCode – Dungeon Game (Java)”

  1. 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?

  2. 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

  3. 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.

  4. Attempt this problem if you want to brush up your DP concepts.. Very Nice problem.. and even perfect solution.. Thumbs Up 🙂

  5. The purpose of the game is for a Knight(K) to traverse a dungeon full of threats and power-ups to rescue the princess(P). You can find the full problem description on oj.leetcode.com in the problem “Dungeon Game”. It is not surprising you can’t find the problem because I (yes, I am the creator of this problem on leetcode) wrote the original problem description in my own language, and apparently it hasn’t gain enough popularity on the web yet.

  6. Is it possible to have more information about the game itself? (the purpose of the game, the meaning of the numbers, meaning of K, meaning of P). I was not able to look it up online

Leave a Comment