Day Title series: 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.

If it's fun to play, it's hard to do what you want.

Ref ideas and code from http://blog.csdn.net/yangliuy/article/details/42643265

Here's the idea of copying someone else.

"Thinking Analysis: This problem is easy to think of the idea is to use search DFS or BFS solution, given the starting point and 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 each expansion of the branch when the current health status is always greater than 0 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 simple, that is, after the initialization from the right-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. ”

The last return value +1, such as input [[0]]

 Public classSolution { Public intCALCULATEMINIMUMHP (int[] Dungeon) {        intm =dungeon.length; intn = dungeon[0].length; int[] DP =New int[M][n]; Dp[m-1][n-1] = Math.max (0, 0-dungeon[m-1][n-1]);  for(inti=m-2;i>=0;i--) {Dp[i][n-1] = Math.max (0,dp[i+1][n-1]-dungeon[i][n-1]); }         for(intj=n-2;j>=0;j--) {dp[m-1][J] = Math.max (0,dp[m-1][j+1]-dungeon[m-1][j]); }         for(inti=m-2;i>=0;i--){             for(intj=n-2;j>=0;j--) {Dp[i][j]= Math.max (0,math.min (dp[i+1][j],dp[i][j+1])-Dungeon[i][j]); }        }        returnDp[0][0]+1; }}

Day Title series: 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.