zoukankan      html  css  js  c++  java
  • leetcode 174. 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)

     

    Note:

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

    题目大意:

    一些恶魔抓住了公主(P)并将她关在了地下城的右下角。地下城是由 M x N 个房间组成的二维网格。我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过对抗恶魔来拯救公主。

    骑士的初始健康点数为一个正整数。如果他的健康点数在某一时刻降至 0 或以下,他会立即死亡。

    有些房间由恶魔守卫,因此骑士在进入这些房间时会失去健康点数(若房间里的值为负整数,则表示骑士将损失健康点数);其他房间要么是空的(房间里的值为 0),要么包含增加骑士健康点数的魔法球(若房间里的值为正整数,则表示骑士将增加健康点数)。

    为了尽快到达公主,骑士决定每次只向右或向下移动一步。

    编写一个函数来计算确保骑士能够拯救到公主所需的最低初始健康点数。

    例如,考虑到如下布局的地下城grid,如果骑士遵循最佳路径 右 -> 右 -> 下 -> 下,则骑士的初始健康点数至少为 7。

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

    说明:

    骑士的健康点数没有上限。

    任何房间都可能对骑士的健康点数造成威胁,也可能增加骑士的健康点数,包括骑士进入的左上角房间以及公主被监禁的右下角房间。

    思路:动态规划的逆推解法。

     我们考虑地下城只有一个房间,即只有例子中右下角(下标索引为$(2,2)$)关着公主的房间,骑士为了救出公主,在离开这个房间时健康点数至少需要为1,这个房间的值为-5,那么进入这个房间时需要的健康点数至少需要 $1-(-5)=6$, 如果这个房间的值为5,那么$1-5=-4$,其实点数必须大于0,此时进入这个房间时需要的健康点数至少需要1($max(1-5, 1)$)。我们简记为dp[2][2]=6,表示从位置(2,2)进入,能够拯救到公主所需的最低初始健康点数。

    我们考虑骑士从(2,1)位置进入,能够拯救公主所需的最低初始健康点数dp[2][1]。此时只能往右走,dp[2][1] = max(dp[2][2] - 30, 1) = 1,即进入(2,1)时,骑士最低初始健康点数为1

    考虑骑士从(1,2)位置进入,能够拯救公主所需的最低初始健康点数dp[1][2]。此时只能往下走,dp[1][2] = max(dp[2][2] - 1, 1) = 5,即进入(1,2)时,骑士最低初始健康点数为5

    考虑骑士从(1,1)位置进入,能够拯救公主所需的最低初始健康点数dp[1][1]。此时骑士为了拯救公主,可以选择往右或者往下走,因此有

    dp[1][1] = max(min(dp[2][1], dp[1][2]) - grid[1][1], 1)

    得到一般情况的状态转移方程:dp[i][j] = max(min(dp[i + 1][j], dp[i][j +1]) - grid[i][j], 1) . ($i in [0,M-2], j in [0, N - 2]$)

    边界条件:dp[M - 1][j] = max(dp[M - 1][j + 1] - grid[M - 1][j], 1) ($j in [0, N - 2]$)

    dp[i][N - 1] = max(dp[i + 1][N - 1] - grid[i][N - 1], 1) ($i in [0, M - 2]$)


    C++代码如下:

    int calculateMinimumHP(vector<vector<int>>& dungeon) {
            int m = dungeon.size();
            if (m == 0)
                return 1;
            int n = dungeon[0].size();
            vector<vector<int> > dp(m, vector<int>(n, 0));
            dp[m - 1][n - 1] = max(1 - dungeon[m - 1][n - 1], 1); //初始化
            //计算最后一行
            for (int j = n - 2; j >= 0; --j) {
                dp[m - 1][j] = max(dp[m - 1][j + 1] - dungeon[m - 1][j], 1);
            }
            //计算最后一列
            for (int i = m - 2; i >= 0; --i) {
                dp[i][n - 1] = max(dp[i + 1][n - 1] - dungeon[i][n - 1], 1);
            }
            //一般情况
            for (int i = m - 2; i >= 0; i--) {
                for (int j = n - 2; j >= 0; j--) {
                    dp[i][j] = max(min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j], 1);
                }
            }
            return dp[0][0];
        }

    进一步可以简化讨论最后一行和最后一列的情况:

     1 int calculateMinimumHP(vector<vector<int>>& dungeon) {
     2         int m = dungeon.size();
     3         if (m == 0)
     4             return 1;
     5         int n = dungeon[0].size();
     6         vector<vector<int> > dp(m + 1, vector<int>(n + 1, INT_MAX));
     7         dp[m][n - 1] = 1;
     8         dp[m - 1][n] = 1;
     9         for (int i = m - 1; i >= 0; i--) {
    10             for (int j = n - 1; j >= 0; j--) {
    11                 dp[i][j] = max(min(dp[i][j + 1], dp[i + 1][j]) - dungeon[i][j], 1);
    12             }
    13         }
    14         return dp[0][0];
    15     }
  • 相关阅读:
    别人走的路--2
    win7下80端口被(Pid=4)占用的解决方法
    实习第一天原来是配置环境
    api接口大全
    java计算两个日期之间相隔的天数
    【转】overload与override的区别
    Overload和Override的区别?
    浅析Java中的final关键字
    JAVA中的finalize()方法
    封装
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/13192971.html
Copyright © 2011-2022 走看看