174. Dungeon Game
来源:互联网 发布:淘宝网小米max手机套 编辑:程序博客网 时间:2024/06/09 19:53
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
.
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.
int calculateMinimumHP(int** dungeon, int dungeonRowSize, int dungeonColSize) { for(int i=dungeonRowSize-1;i>=0;i--){ for(int j=dungeonColSize-1;j>=0;j--){ if(i==dungeonRowSize-1&&j==dungeonColSize-1){ continue; } else if(i==dungeonRowSize-1){ if(dungeon[i][j+1]<0){ dungeon[i][j]+=dungeon[i][j+1]; } } else if(j==dungeonColSize-1){ if(dungeon[i+1][j]<0){ dungeon[i][j]+=dungeon[i+1][j]; } } else{ int max=dungeon[i+1][j]>=dungeon[i][j+1]?dungeon[i+1][j]:dungeon[i][j+1]; if(max<0){ dungeon[i][j]+=max; } } } } return dungeon[0][0]>=0?1:1-dungeon[0][0];}可以从终点往起点倒推,对迷宫里(除终点外)每一个点计算从这个点走到终点至少要扣掉多少血,推到起点也就得到结果了。因为从下往上从右往左一行一行遍历,所以每一个点的右和下都在之前算过,因此可以知道下一步是走→好还是走↓好(比如一个值是-5,一个是-2,当然是-2好了,因为-2意味着走这一步之前血量剩下3就可以走到终点而不挂)。知道了下一步怎么走就可以计算当前这个点的数值了:如果下一步是个正数,意味着只要当前角色的血量大于1,就能走到终点而不挂,那就不用改当前点的数值了(如果这一步是BUFF,反正死不了改不改都一样;如果是DEBUFF,那就更不能改,后面能不能活至少要先过了这一步再说,比方这一步有个-10的debuff,下一步是5,角色任然需要11的血量才能走过这一步,因此不改);如果下一步是个负数,那么要在当前数值(也就是BUFF或者DEBUFF的数值)上加上这个负数,意味着从这个点走到终点,至少还要扣掉这么多血。
- [leetcode] 174.Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- 174. Dungeon Game
- 174. Dungeon Game
- LeetCode-174.Dungeon Game
- [LeetCode] 174. Dungeon Game
- 174. Dungeon Game
- 174. Dungeon Game
- LeetCode 174. Dungeon Game
- LeetCode 174. Dungeon Game
- 174. Dungeon Game
- [LeetCode]174. Dungeon Game
- 174. Dungeon Game
- leetCode 174. Dungeon Game
- 174. Dungeon Game
- Leetcode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- MAVEN创建项目后缺少目录
- 二分图的最大匹配、完美匹配和匈牙利算法
- 【Unity Shader】UnityShader基础
- VMware Ubuntu安装详细过程
- 禁止nautilus自动remount弹窗
- 174. Dungeon Game
- nginx简易教程
- 我的高大上专有名词
- Deep Learning的学习实践 4 -- DBN
- jfreechart生成柱状图,x轴间隔显示
- 更新DNS修改host
- Volley源码学习笔记_ExecutorDelivery
- lua当作配置文件
- Android之视频播放使用MediaPlayer+SurfaceView代替ViedoView控件