LeetCode 174. Dungeon Game(DP)

题目

题意:每个格子里都有数字,负数代表你会少血,正数代表你会加血,当你的血量为0的时候就死了,从左上角出发,到右下角,问你一开始最少的血量是多少。整个过程中不能有血量为0的情况。

题解:只能走下或者走右。这种有向无环图,八成都是动态规划。但是如果从左上角开始规划,有很多情况要考虑。从右下角开始规划,才可以。

dp[i][j]代表从i,j到右下角,最低需要多少血量。

class Solution {
public:
    int dp[1005][1005];
    int calculateMinimumHP(vector<vector<int>>& dungeon) {

       for(int i=0;i<dungeon.size();i++)
       {
           for(int j=0;j<dungeon[i].size();j++)
           {
               dp[i][j] = 99999999;
           }
       }

       for(int i=dungeon.size()-1;i>=0;i--)
       {
          for(int j=dungeon[i].size()-1;j>=0;j--)
          {

              if(i!=dungeon.size()-1)
              {
                  if(dungeon[i][j]<0)
                  {
                      int x = dp[i+1][j] + dungeon[i][j]*-1;
                      if(dp[i+1][j]==0)
                          x ++;
                      dp[i][j] = min(dp[i][j],x);

                  }
                  else
                  {
                      int x = (dp[i+1][j] - dungeon[i][j])<0?0:(dp[i+1][j] - dungeon[i][j]);
                      dp[i][j] = min(dp[i][j],x);
                  }
              }

              if(j!=dungeon[i].size()-1)
              {
                  if(dungeon[i][j]<0)
                  {
                      int x = dp[i][j+1] + dungeon[i][j]*-1;
                      if(dp[i][j+1]==0)
                          x++;
                      dp[i][j] = min(dp[i][j],x);

                  }
                  else
                  {
                      int x= (dp[i][j+1] - dungeon[i][j])<0?0:(dp[i][j+1] - dungeon[i][j]);
                      dp[i][j]=min(dp[i][j],x);
                  }
              }

              if(j==dungeon[i].size()-1&&i==dungeon.size()-1)
              {
                  if(dungeon[i][j]<0)
                  {
                      dp[i][j]=dungeon[i][j]*-1 +1;
                  }
                  else if(dungeon[i][j]==0)
                  {
                      dp[i][j]=1;
                  }
                  else
                      dp[i][j]=0;
              }
          }
       }

          return max(dp[0][0],1);

    }

};

原文地址:https://www.cnblogs.com/dacc123/p/12236149.html

时间: 2024-10-10 01:28:02

LeetCode 174. Dungeon Game(DP)的相关文章

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

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>&g

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]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. 地下城游戏 解题报告

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

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,

[LeetCode] Triangle(&#39;Bottom-up&#39; DP)

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

[LeetCode] Distinct Subsequences(DP)

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

[LeetCode] Decode Ways(DP)

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded