博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Dungeon Game 地牢游戏
阅读量:6989 次
发布时间:2019-06-27

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

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:

  • 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.

 

Credits:

Special thanks to  for adding this problem and creating all test cases.

这道王子救公主的题还是蛮新颖的,我最开始的想法是比较右边和下边的数字的大小,去大的那个,但是这个算法对某些情况不成立,比如下面的情况:

1 (K) -3 3
0 -2 0
-3 -3 -3 (P)

如果按我的那种算法走的路径为 1 -> 0 -> -2 -> 0 -> -3, 这样的话骑士的起始血量要为5,而正确的路径应为 1 -> -3 -> 3 -> 0 -> -3, 这样骑士的骑士血量只需为3。无奈只好上网看大神的解法,发现统一都是用来做,建立一个和迷宫大小相同的二维数组用来表示当前位置出发的起始血量,最先初始化的是公主所在的房间的起始生命值,然后慢慢向第一个房间扩散,不断的得到各个位置的最优的起始生命值。递归方程为: 递归方程是dp[i][j] = max(1, min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j]). 代码如下:

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

本文转自博客园Grandyang的博客,原文链接:,如需转载请自行联系原博主。

你可能感兴趣的文章
机器学习领域的几种主要学习方式
查看>>
数据库存储时间的时区问题
查看>>
《Python Cookbook(第2版)中文版》——1.16 替换字符串中的子串
查看>>
《Python Cookbook(第2版)中文版》——1.15 扩展和压缩制表符
查看>>
使用DNSCrypt来加密您与OpenDNS之间的通信
查看>>
支付宝体验设计精髓
查看>>
如何在 Linux 上永久挂载一个 Windows 共享
查看>>
《MapReduce 2.0源码分析与编程实战》一2.2 数据操作
查看>>
springboot(七):springboot+mybatis多数据源最简解决方案
查看>>
《Unity着色器和屏幕特效开发秘笈(原书第2版)》一2.3 使用包装数组
查看>>
《jQuery移动开发》—— 第 1 章 理解jQuery
查看>>
使用Docker做开发的建议团队工作流
查看>>
当Kubernets遇上阿里云 -之七层负载均衡(一).
查看>>
《C语言及程序设计》资料——C语言中转义字符
查看>>
什么是线程安全
查看>>
Windows 去除打开exe文件安全警告
查看>>
mac系统下nginx的详细安装过程及使用(适合新手)
查看>>
C++网络服务器编程的学习路线?
查看>>
Java servlet判断是否是移动设备
查看>>
C# 批量复制文件
查看>>