[Leetcode] 64. 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.

boke_0.jpg

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.

解題之法

class Solution {
public:
    int calculateMinimumHP(vector<vector<int> > &dungeon) {
        int m = dungeon.size();
        int n = dungeon[0].size();
        int dp[m][n];
        dp[m - 1][n - 1] = max(1, 1 - dungeon[m - 1][n - 1]);
        for (int i = m - 2; i >= 0; --i) {
            dp[i][n - 1] = max(1, dp[i + 1][n - 1] - dungeon[i][n - 1]);
        }
        for (int j = n - 2; j >= 0; --j) {
            dp[m - 1][j] = max(1, dp[m - 1][j + 1] - dungeon[m - 1][j]);
        }
        for (int i = m - 2; i >= 0; --i) {
            for (int j = n - 2; j >= 0; --j) {
                dp[i][j] = max(1, min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]);
            }
        }
        return dp[0][0];
    }
};

分析

這道王子救公主的題還是蠻新穎的,我最開始的想法是比較右邊和下邊的數(shù)字的大小豌鸡,去大的那個嘿般,但是這個算法對某些情況不成立段标,比如下面的情況:
1 (K) -3 3
0 -2 0
-3 -3 -3 (P)

如果按我的那種算法走的路徑為 1 -> 0 -> -2 -> 0 -> -3, 這樣的話騎士的起始血量要為5,而正確的路徑應(yīng)為 1 -> -3 -> 3 -> 0 -> -3, 這樣騎士的騎士血量只需為3博个。無奈只好上網(wǎng)看大神的解法怀樟,發(fā)現(xiàn)統(tǒng)一都是用動態(tài)規(guī)劃Dynamic Programming來做,建立一個和迷宮大小相同的二維數(shù)組用來表示當(dāng)前位置出發(fā)的起始血量盆佣,最先初始化的是公主所在的房間的起始生命值往堡,然后慢慢向第一個房間擴散,不斷的得到各個位置的最優(yōu)的起始生命值共耍。遞歸方程為: 遞歸方程是dp[i][j] = max(1, min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j]).

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末虑灰,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子痹兜,更是在濱河造成了極大的恐慌穆咐,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件字旭,死亡現(xiàn)場離奇詭異对湃,居然都是意外死亡,警方通過查閱死者的電腦和手機遗淳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門拍柒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人屈暗,你說我怎么就攤上這事拆讯。” “怎么了养叛?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵种呐,是天一觀的道長揭厚。 經(jīng)常有香客問我吃谣,道長锰瘸,這世上最難降的妖魔是什么僵控? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任仙蛉,我火速辦了婚禮盒使,結(jié)果婚禮上捣作,老公的妹妹穿的比我還像新娘衣摩。我一直安慰自己卜录,他們只是感情好戈擒,可當(dāng)我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著艰毒,像睡著了一般筐高。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天柑土,我揣著相機與錄音蜀肘,去河邊找鬼。 笑死稽屏,一個胖子當(dāng)著我的面吹牛扮宠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播狐榔,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼坛增,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了薄腻?” 一聲冷哼從身側(cè)響起收捣,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎庵楷,沒想到半個月后罢艾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡尽纽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年咐蚯,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片弄贿。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡仓蛆,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出挎春,到底是詐尸還是另有隱情,我是刑警寧澤豆拨,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布直奋,位于F島的核電站,受9級特大地震影響施禾,放射性物質(zhì)發(fā)生泄漏脚线。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一弥搞、第九天 我趴在偏房一處隱蔽的房頂上張望邮绿。 院中可真熱鬧,春花似錦攀例、人聲如沸船逮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挖胃。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間酱鸭,已是汗流浹背吗垮。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留凹髓,地道東北人烁登。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像蔚舀,于是被迫代替她去往敵國和親饵沧。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容