国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Leetcode 174 Dungeon Game

2019-11-08 03:05:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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)-33
-5-101
1030-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.勇者斗惡龍

簡(jiǎn)單DP,可以用滾動(dòng)數(shù)組優(yōu)化

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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 邵东县| 海南省| 桂阳县| 蓬莱市| 大荔县| 拉萨市| 奇台县| 涪陵区| 中方县| 延长县| 济源市| 莆田市| 肥西县| 巢湖市| 龙江县| 沙河市| 巧家县| 郎溪县| 重庆市| 勃利县| 汶川县| 银川市| 云林县| 中牟县| 惠来县| 乐山市| 杂多县| 晋州市| 彭山县| 岱山县| 油尖旺区| 安达市| 镇坪县| 衡山县| 张掖市| 万宁市| 竹山县| 曲阜市| 荔浦县| 阿图什市| 滦平县|