Dungeon Game -- latched

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.

基本思路:

动态规划

设health[i][j]  为走进dungeon[i][j]的初始血量,且该个血量将能维持到骑士足以走完右下角。

已知条件:骑士走完右下角至少要剩一滴血。即health[m][n-1] = 1。   m为dungeon行数。也可以设health[m-1][n] = 1。

此值表示走完右下角的剩余血量。同时也是从该右下角向右,或者向下,走到另一格时的初始血量。 当然此两格是虚拟的,地牢中不存在。或者形象的说,从右下角向右或者向下走出地牢后,剩余的血量。

从此点,可以倒推出health[0][0]。

骑士只能向右,向下右移动。  要知道当前位置的初始血量,只需要知道其右和其下的初始血量,就可以反推出。即

health[i][j] = min(health[i+1][j], health[i[j+1])  - dungeon[i][j]

由于骑士要时刻保持血量至少为1. 上面可以改为:

health[i][j] = max(1, min(health[i+1][j], health[i[j+1])  - dungeon[i][j])

由于递推时只需要其右和其下,两个位置, 可以使用滚动数组,用一维替换掉二维数组。

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]) - dungeon[i][j]);
            }
        }
        return health[0];
    }
};
时间: 2024-10-31 23:05:31

Dungeon Game -- latched的相关文章

【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

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

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i