letcode - Dungeon Game

题目:

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)
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 (negative integers) upon entering these rooms; other rooms are either empty (0‘s) or contain magic orbs that increase the knight‘s health (positive integers).

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 least 7 if he follows the optimal 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 room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.

分析:

动态规划的题,时间复杂度和空间复杂度都是O(m*n),其中空间复杂度可以利用滚动数组优化为O(min(m,n));

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]-dungeon[i][j];
                }
                else
                {
                    tmp=dp[i][j+1]-dungeon[i][j];
                }
                 dp[i][j]=max(1,tmp);
            }
        }
        return dp[0][0];
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 06:01:02

letcode - Dungeon Game的相关文章

【LeetCode】Dungeon Game 解题报告【Solution】

[题目] 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 h

暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot m

hdu 2251 Dungeon Master bfs

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

[LeetCode] 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) was initially positioned in the top-left room and must fight his wa

Dungeon Game (GRAPH - DP)

QUESTION 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 fig

Dungeon Master(BFS)

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonal

python letcode 1

开始刷 letcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 1. Two Sum. class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ tmp = [] for i, j in enumerate(nums): tmp.append((

UVA532 Dungeon Master

问题链接:UVA532 Dungeon Master. 题意简述:三维空间地牢(迷宫),每个点由'.'(可以经过).'#'(墙).'S'(起点)和'E'(终点)组成.移动方向有上.下.左.右.前和后6个方向.每移动一次耗费1分钟,求从'S'到'E'最快走出时间.不同L层,相同RC处是连通的. 问题分析:一个三维迷宫,典型的BFS问题.在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少. 程序中,使用了一个队列来存放中间节点,但是每次用完需要清空.需要注意的一点,为了编

Dungeon Master

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock