Letcode-dungeon Game

Source: Internet
Author: User

Topic:

Dungeon Game

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.

Analysis:

The problem of dynamic programming, time complexity and space complexity are all O (m*n), in which the spatial complexity can be optimized to O (min (m,n) by using the rolling array;

Class Solution {Public:int calculateminimumhp (vector<vector<int>>& Dungeon) {if (Dungeon.empty ( ) ||        Dungeon[0].empty ()) return-1;        int rows=dungeon.size (), cols=dungeon[0].size ();        Vector<vector<int>> DP (rows,vector<int> (cols,0));        Dp.back (). Back () =dungeon.back (). Back () >=0?1:1+ ( -1) *dungeon.back (). back ();        int begini=rows-1,beginj=cols-2;            if (cols==1) {if (rows==1) return dp[0][0];                else {begini=rows-2;            beginj=0;            }} for (int i=begini;i>=0;--i) {for (int j=i==begini?beginj:cols-1;j>=0;--j)                {int tmp;                if (i<rows-1 && j<cols-1) {tmp=min (dp[i+1][j],dp[i][j+1])-dungeon[i][j]; } else if (i<rows-1) {Tmp=dp[i+1][j]-dungeonI                [j];                } else {tmp=dp[i][j+1]-dungeon[i][j];            } dp[i][j]=max (1,TMP);    }} return dp[0][0]; }};





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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