Dungeon Game--latched

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 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)


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

Basic ideas:

Dynamic planning

Set Health[i][j] To enter the initial amount of dungeon[i][j], and the amount of the blood will be able to maintain the knight enough to walk through the lower right corner.

Known conditions: The Knight has left at least one drop of blood left in the lower right corner.   i.e. health[m][n-1] = 1. M is the number of dungeon rows. can also be set health[m-1][n] = 1.

This value indicates the amount of remaining blood left in the lower right corner. It is also the initial amount of blood from the lower right corner, or downward, to the other. Of course, the two grids are virtual and do not exist in dungeons. Or the image says, from the lower right corner to the right or down out of the dungeon, the remaining amount of blood.

From this point, you can pour out health[0][0].

The knight can only move right and down. To know the initial amount of blood in the current position, just know its right and the initial amount of blood below it, it can be rolled back. That

Health[i][j] = min (Health[i+1][j], health[i[j+1])-DUNGEON[I][J]

Because the knight has to keep the blood at least 1 at all times. The above can be changed to:

HEALTH[I][J] = max (1, Min (Health[i+1][j], health[i[j+1])-dungeon[i][j])

Since recursion requires only its right and bottom, two positions, you can use a scrolling array and replace the two-dimensional array with one-dimensional.

Class Solution {public:    int calculateminimumhp (vector<vector<int>>& dungeon) {        if ( Dungeon.empty () | | Dungeon[0].empty ())            return 0;        const int m = Dungeon.size ();        const int n = dungeon[0].size ();        Vector<int> Health (n+1, Int_max);        Health[n-1] = 1;        for (int i=m-1; i>=0; i--) {            for (int j=n-1; j>=0; j--) {                health[j] = max (1, Min (Health[j], health[j+1])-D UNGEON[I][J]);            }        }        return health[0];}    ;

Dungeon Game--latched

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.