博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
174. Dungeon Game
阅读量:6715 次
发布时间:2019-06-25

本文共 2177 字,大约阅读时间需要 7 分钟。

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) -3 3
-5 -10 1
10 30 -5 (P)

 

Notes:

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

       

      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.

转载于:https://www.cnblogs.com/jxr041100/p/7909240.html

你可能感兴趣的文章
微软私有云分享(R2)18Windows Azure Pack 命令行安装
查看>>
基于ArcGIS10.0和Oracle10g的空间数据管理平台十五(C#开发)-空间数据导出
查看>>
DB2 应用
查看>>
第十六章 为什么说张清“虎头蛇尾”
查看>>
ShiftOperators.cs
查看>>
C#中的预处理命令
查看>>
Assistance Required(打表)
查看>>
使用Ajax的Time实现倒计时功能
查看>>
Solr字段配置错误
查看>>
Android ActionBar详解(二):ActionBar实现Tabs标签以及下拉导航
查看>>
使用windbg查看DependencyObject的属性
查看>>
IE首页被篡改(手动修复)
查看>>
基于FPGA的图像处理(二)--System Generator入门
查看>>
DIV+CSS 入门
查看>>
UVa 213 Message Decoding(World Finals1991,串)
查看>>
Everything search syntax
查看>>
BZOJ 3211 弗洛拉前往国家 树阵+并检查集合
查看>>
Windows下一个SlikSVN使用
查看>>
DataTable.Compute 性能慢的问题
查看>>
分层是一种思想
查看>>