国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 常山县| 高要市| 剑阁县| 社会| 方山县| 寿宁县| 大姚县| 遂宁市| 云浮市| 调兵山市| 朝阳县| 乐至县| 叙永县| 南乐县| 阳曲县| 垦利县| 景德镇市| 铜山县| 陆河县| 曲沃县| 准格尔旗| 崇礼县| 台中县| 聂荣县| 白城市| 合水县| 龙口市| 乾安县| 喀什市| 溆浦县| 盐城市| 鹤山市| 新巴尔虎右旗| 长沙市| 张家口市| 抚远县| 江阴市| 阿拉尔市| 泾源县| 大理市| 海盐县|