LeetCode174 Dungeon Game

Source: Internet
Author: User

Describe:

He 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 the 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  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'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
ten 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 P Rincess is imprisoned.

Simple statement is that there is an MXN lattice, each lattice has a number, or positive or negative (also can be 0), to go from the upper left to the lower right corner, can only go right or down, each walk a lattice to add the number of lattice, if midway into less than equals 0 failure, ask the initial minimum number can go to the lower right corner.

The first thought from the top left corner to the right or down to find the largest, until the lower right corner, but this method is obviously wrong, such as:

-1 -2 -100 -1000
-3 -20 1500 -1000
4 3 2 1

The correct path should be -1>>-3>>4>>3>>2>>1, but this will choose -1>>-2>>-20>>1500>>2 >>1 this path because it does not take into account the minimum value in the process, so that it only reaches the lower-right corner so that the value is maximum.

To make the test instructions can be satisfied with the dichotomy, let you in the upper left corner to carry a number, such as 10, you take this number to see if you can go to the lower right corner, in the path can not appear less than 0, which becomes the search for a path to reach, select 10 if the arrival is greater than 0, 10 this "carrying number" can , two is divided into 5, carrier 5 to find the path, can not find on the two into (5+10)/2, and so on, and so on, will eventually be able to find the problem required by the minimum value. This method is also dependent on the initial "carrying number" you have chosen, which is obviously not the optimal solution.

We can consider starting from the lower right corner of the upper-left corner to find the path, Dp_table[i][j] represents the first row J in this lattice to reach the lower right corner to carry the smallest number, such as test instructions to the 3x3 table, first observe the following several numbers

-10 1
30 -5

At 5 at the beginning of at least 5, to arrive from 1 to need 5-1=4, from 30 to reach the need to 5-30=-25, that you do not need to carry anything to reach the end, that is 0, in turn, in 10 except, 10 right is 4, the bottom is 0,

(-10) = 14, which means you need to take 14 to the right, while the downward need 0-(-10) = 10, obviously we have small, then we dynamically plan the table is:

10 4
0 5

We can list the formulas we need: Dp_table[i][j]=math. Max (Math.     Min (dp_table[i][j+1],dp_table[i+1][j])-dungeon[i][j],0); DUNGEON[I][J] represents the number in the initial table.

The result is now very clear, that is dp_table[0][0]+1; The following is the Java code implementation.

1   Public  intCALCULATEMINIMUMHP (int[] Dungeon) {2         intM=dungeon.length-1;3         intN=dungeon[0].length-1;4         int[] dp_table=New int[M+1] [N+1];5 6Dp_table[m][n]=math.max (0-dungeon[m][n],0);//Initial last element7          for(inti=m-1;i>=0;i--) {//Initial rightmost column8Dp_table[i][n]=math.max (dp_table[i+1][n]-dungeon[i][n],0);9         }Ten          for(intj=n-1;j>=0;j--) {//Initialize the bottom row OneDp_table[m][j]=math.max (dp_table[m][j+1]-dungeon[m][j],0); A         } -          for(inti=m-1;i>=0;i--)//Initialize the remaining in the Dynamic planning table -              for(intj=n-1;j>=0;j--){ theDp_table[i][j]=math.max (Math.min (dp_table[i][j+1],dp_table[i+1][j])-dungeon[i][j],0); -             } -         returnDp_table[0][0]+1; -}
View Java Code

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