"Original" Leetcodeoj---dungeon Game problem Solving report

Source: Internet
Author: User

Original title Address:

https://oj.leetcode.com/problems/dungeon-game/

Topic content:

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.

Method:

Alas, the leetcode is getting more and more water. This question is relatively simple DP problem, Mark Medium also line, Mark hard a bit let people disappointed.

Step into the chase.

Define state Dp[i][j] to enter i,j coordinates " before", at least how many HP needed to reach the end point from i,j coordinates.

Because in a coordinate, not walking right is going down, so:

Set next = min (Dp[i + 1][j], Dp[i][j + 1]), pos = Dungeon[i][j]

DP[I][J] = pos >= next? 1:next-pos

A brief talk. The first value of next is the minimum HP value required for the coordinates of right and down two in the i,j coordinates that are able to reach the end point at a minimum of HP. (a bit of a mouthful.) For example, to the right, you need to go to the right before there are 10 hp to finally reach the end, and go down will need 5 points, then the value of next is 5)

Second, if the i,j coordinates can provide enough HP assurance to go to next, then there are 1 points before entering the i,j coordinates, HP is sufficient to ensure that the line is not dead, instead, it is necessary to use Next-pos to calculate the HP points needed before entering the i,j coordinates.

The implementation of a one-dimensional DP array can be used to simulate the entire process, do not need to really apply for a two-dimensional array. Specific can refer to the code, if not understand can leave a message, I will talk about.

Specific code:

Class Solution {Public:int CALCULATEMINIMUMHP (vector<vector<int> > &dungeon) {int ylength = du Ngeon.size ();        How many sub-array if (ylength = = 0) return 0; int xlength = Dungeon[0].size ();        How many elements that Sub-array contains if (xlength = = 0) return 0;        int max = ~ (1 << 31);        vector<int> Res;        for (int i = 0; i < xlength; i + +) {res.push_back (0);                } for (int i = ylength-1; I >= 0; I--) {for (int j = xlength-1; J >= 0; J--) {                int x = j + 1;                int y = i + 1; int right = x < xlength?                RES[X]: max; int down = y < ylength?                RES[J]: max;  if (right = = max && down = = max) {Res[j] = Dungeon[ylength-1][xlength-1] >= 0? 1:1- DUNGEON[YLENGTH-1][XLENGTH-1];                    Final point} else {int tmp = right > down?                    Down:right;                    int pos = dungeon[i][j];                       if (POS >= tmp) {res[j] = 1;                    } else {res[j] = Tmp-pos;    }}}} return res[0]; }};

  

"Original" Leetcodeoj---dungeon Game problem Solving report

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.