Leetcode 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 T O 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 right->-down .

-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.
thought Analysis: This problem is easy to think of the idea is to use search DFS or BFS solution, given the starting point and the end point, we can search all the way from the beginning to the end of the path, and then greedy to save the smallest path of the sum of weights, and to ensure that the current state of health is always greater than 0 when expanding the branch. But this is not a good solution, the complexity of the time is too high. In fact, this is similar to the unique path, from the upper left to the lower right corner, and each grid can only go down or to the right, that is, each lattice only the right or bottom two adjacent lattice. We can consider using dynamic programming solutions. The key is how to define a DP array, which is a bit different than the definition of a DP array in unique path. Assuming that the given mesh is m*n, we need to define dp[][] to represent the minimum health required from (I,J) to the end point (m-1,n-1), then the recursive equation is dp[i][j] = max (min (dp[i][j+1], dp[i+1][j])-dungeon[i ][J], 0), if dungeon[i][j] is positive, subtract a positive number, and the initial required health is reduced. The same can be understood dungeon[i][j] is a negative case. and 0 Maximum guarantee that the initial health value is non-negative. The code implementation is very simple, that is, after the initialization of the free-to-left form, a typical two-dimensional DP problem practice. About this kind of grid graph theory problem can use DFS,BFS,DP and classical graph algorithm such as Dijkstra algorithm, Flod algorithm and so on to do, need to do this kind of topic more, choose different algorithm according to different circumstance, increase experience of doing this kind of topic.
AC Code
public class Solution {public int calculateminimumhp (int[][] Dungeon) {//two-D space DP problem int m = Dungeon.lengt        H                int n = dungeon[0].length; 0225 int [] [] DP = new INT[M][N];//DP[I][J] represents the minimum health required from (I,J) to the destination (m-1,n-1)//Initialize dp[m-1][n-1] = Math.        Max (0-dungeon[m-1][n-1], 0);        for (int i = m-2; I >= 0; i--) {dp[i][n-1] = Math.max (dp[i+1][n-1]-dungeon[i][n-1], 0);        } for (int i = n-2; I >= 0; i--) {Dp[m-1][i] = Math.max (dp[m-1][i+1]-dungeon[m-1][i], 0); }//From the bottom up, right-to-left forms the//DP equation for dp[i][j] = min (dp[i][j+1], dp[i+1][j])-dungeon[i][j] and 0 for the maximum for (int i = m 2; I >= 0; i--) {for (int j = n-2; J >= 0; j--) {Dp[i][j] = Math.max (Math.min (dp[i][j+1], dp[i+1][j])-D            UNGEON[I][J], 0);        }} return dp[0][0] + 1; 0234}}


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