"Leetcode" Dungeon Game Problem Solving Report "solution"

Source: Internet
Author: User

Topic

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 Optim Al path RIGHT-> RIGHT -> DOWN -> 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.
Test instructions

There are MXN, the Queen in the lower right corner, the warrior in the upper left corner, now the warrior to save the queen, each step can go down or to the right, each lattice in the number of the Warriors to the place to consume or increase the magic, (negative means consumption, positive increase), whenever the Warrior's magic is less than equal to 0, the warrior immediately How much magic should the warrior start with at least?

Resolution

Thinking: Dynamic planning

Using a two-dimensional array ans[][] to each lattice, the warrior must at least need the magic at each step, such as ans[i][j] to show that the warrior at [I, j] at least ans[i][j] magic to reach [M, N] to save the Queen.

Tip: Traverse backwards from [M, N] to [1, 1]

Equation of motion: if the current position of magic is greater than or equal to the right/bottom of the required magic, then there is no need for additional magic, otherwise, the warrior will arrive at the place need to have some magic to meet the needs of the right/bottom and the magic.


"Java Code"

public class Solution {public int calculateminimumhp (int[][] dungeon) {int m = dungeon.length;                int n = dungeon[0].length;                int[][] ans = new int[m][n]; Initialize last row and last column ans[m-1][n-1] = dungeon[m-1][n-1] > 0?        0:-dungeon[m-1][n-1]; for (int i = m-2; I >= 0; i--) {ans[i][n-1] = dungeon[i][n-1] >= ans[i + 1][n-1]? 0:ans[i + 1        ][N-1]-dungeon[i][n-1]; } for (int j = n-2; J >= 0; j--) {ans[m-1][j] = Dungeon[m-1][j] >= ans[m-1][j + 1]? 0:        Ans[m-1][j + 1]-dungeon[m-1][j]; }//from the lower right corner to the upper left corner of the for (int i = m-2; I >= 0; i--) {for (int j = n-2; J >= 0; j--)                {int down = Dungeon[i][j] >= ans[i + 1][j]? 0:ans[i + 1][j]-dungeon[i][j]; int right = Dungeon[i][j] >= ans[i][j + 1]?                0:ans[i][j + 1]-dungeon[i][j]; ANS[I][J] = Math.min (Down, righT);    }}//To ensure that the warrior is alive, at least 1 mana return ans[0][0] + 1 is required; }}


"Leetcode" Dungeon Game Problem Solving Report "solution"

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.