[动态规划] leetcode 174 Dungeon Game

problem:https://leetcode.com/problems/dungeon-game

看了这道题的tag我用了二分 + 简化dp(只需求特定血量能否达到)来做,能过但是速度好慢。一看评论区发现大家都是用纯dp过的,我哭了。

class Solution {
public:
    int m, n;
    bool canSave(vector<vector<int>>& dungeon, int life)
    {
        vector<vector<int>> health(m, vector<int>(n, -1));
        health[0][0] = life + dungeon[0][0];
        for(int i = 0;i < m ;i++)
        {
            for(int j = 0;j < n;j++)
            {
                if(i > 0 && health[i - 1][j] > 0)
                {
                    health[i][j] = health[i - 1][j] + dungeon[i][j];
                }
                if(j > 0 && health[i][j - 1] > 0)
                {
                    health[i][j] = max(health[i][j], health[i][j - 1] + dungeon[i][j]);
                }
            }
        }
        return health[m - 1][n - 1] > 0;
    }
    int calculateMinimumHP(vector<vector<int>>& dungeon) {
        m = dungeon.size();
        if(!m) return 0;
        n = dungeon[0].size();

        int high = 1;
        int low = 1;

        for(int i = 0; i < m; i++)
        {
            for(int j = 0;j < n; j++)
            {
                if(dungeon[i][j] < 0)
                {
                    high += -dungeon[i][j];
                }
            }
        }

        while(low < high)
        {
            int mid = low + (high - low) / 2;
            if(canSave(dungeon, mid))
            {
                high = mid;
            }
            else
            {
                low = mid + 1;
            }
        }
        return low;
    }
};

原文地址:https://www.cnblogs.com/fish1996/p/11331035.html

时间: 2024-10-03 14:05:25

[动态规划] leetcode 174 Dungeon Game的相关文章

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 wa

LeetCode 174. Dungeon Game(DP)

题目 题意:每个格子里都有数字,负数代表你会少血,正数代表你会加血,当你的血量为0的时候就死了,从左上角出发,到右下角,问你一开始最少的血量是多少.整个过程中不能有血量为0的情况. 题解:只能走下或者走右.这种有向无环图,八成都是动态规划.但是如果从左上角开始规划,有很多情况要考虑.从右下角开始规划,才可以. dp[i][j]代表从i,j到右下角,最低需要多少血量. class Solution { public: int dp[1005][1005]; int calculateMinimum

Java for 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 wa

leetcode 174. Dungeon Game 地牢游戏 --------- java

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

leetcode 174. 地下城游戏 解题报告

leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过对抗恶魔来拯救公主. 骑士的初始健康点数为一个正整数.如果他的健康点数在某一时刻降至 0 或以下,他会立即死亡. 有些房间由恶魔守卫,因此骑士在进入这些房间时会失去健康点数(若房间里的值为负整数,则表示骑士将损失健康点数):其他房间要么是空的(房间里的值为 0),要么包含增加骑士健康点数的魔

【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

【leetcode】Dungeon Game (middle)

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

【leetcode】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

[email&#160;protected] [174] Dungeon Game (Dynamic Programming)

https://leetcode.com/problems/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 positione