174. Dungeon Game

Source: Internet
Author: User

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) is initially positioned in the top-left, and must fight his, through the dungeon to rescue The princess.

The knight has an initial health point represented by a positive integer. Drops to 0 or below, he dies immediately.

Some of the rooms is guarded by demons, so the knight loses health (negative integers) upon entering these rooms ; Other rooms is either empty (0 ' s) or contain magic orbs that increase the knight ' s Health (positive int Egers).

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's able to rescue the princess.

For example, given the dungeon below, the initial health of the knight must is at least 7 if he follows the optimal path c0/>.

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

Notes:

      • The Knight ' s Health has no upper bound.
      • Any hostel can contain threats or power-ups, even the first, the knight enters and the Bottom-right, where the Princ ESS is imprisoned.

Classic DP problem, take the map, ensure that each school is greater than or equal to 1

DP[I][J] = max (1, Min (dp[i][j+1], dp[i+1][j]) + dungeon[i][j])

public class Solution {
public int calculateminimumhp (int[][] Dungeon) {
int row = Dungeon.length;
if (row = = 0) {
return 0;
}
int col = dungeon[0].length;
Int[][] dp = new Int[row][col];
DP[ROW-1][COL-1] = Math.max (1,-dungeon[row-1][col-1] + 1);
for (int i = row-2; I >= 0; i--) {
DP[I][COL-1] = Math.max (1, DP [I+1][COL-1]-dungeon[i][col-1]);
}
for (int i = col-2; I >= 0; i--) {
Dp[row-1][i] = Math.max (1, DP [Row-1][i + 1]-dungeon[row-1][i]);
}
for (int i = row-2; I >= 0; i--) {
for (int j = col-2; j>=0; j--) {
int min = math.min (Dp[i+1][j], dp[i][j+1])-dungeon[i][j];
DP[I][J] = Math.max (1, min);
}
}
return dp[0][0];
}
}

174. Dungeon Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.