Leetcode 174: Dungeon Game, leetcodedungeon.

Source: Internet
Author: User

Leetcode 174: Dungeon Game, leetcodedungeon.
Dungeon GameTotal Accepted:332Total Submissions:2130

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) Was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.

The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.

Some of the rooms are guarded by demons, so the knight loses health (NegativeIntegers) upon entering these rooms; other rooms are either empty (0's) Or contain magic orbs that increase the knight's health (PositiveIntegers ).

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 is able to rescue the princess.

For example, given the dungeon below, the initial health of the knight must be at least7If he follows the optimal pathRIGHT-> RIGHT -> DOWN -> DOWN.

-2 (K) -3 3
-5 -10 1
10 30 -5 (P)

Notes:

  • The knight's health has no upper bound.
  • Any room can contain in threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.

[Analysis]

Starting from destination, calculate the number of HP entries to reach the destination.

[Note]

Start from the upper left or lower right?

[CODE]

public class Solution {    public int calculateMinimumHP(int[][] dungeon) {        //valid input.        int m = dungeon.length;        int n = dungeon[0].length;        int[][] d = new int[m][n];                d[m-1][n-1] = minHP(1-dungeon[m-1][n-1]);            for(int i=m-2; i>=0; i--) {        d[i][n-1] = minHP( d[i+1][n-1] - dungeon[i][n-1] );        }        for(int j=n-2; j>=0; j--) {        d[m-1][j] = minHP( d[m-1][j+1] - dungeon[m-1][j]);        }        for(int i=m-2; i>=0; i--) {        for(int j=n-2; j>=0; j--) {        d[i][j] = minHP( Math.min(d[i+1][j], d[i][j+1]) - dungeon[i][j]);        }        }        return d[0][0];    }        private int minHP(int x) {        return x<=0? 1 : x;    }}


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.