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

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

Dungeon Game

 

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.

Java代码;

public class Solution {    public int calculateMinimumHP(int[][] dungeon) {         for(int m = dungeon.length-1;m>=0;m--)           for(int n = dungeon[0].length-1;n>=0;n--){               if((m==dungeon.length-1)&&(n==dungeon[0].length-1)){           //AT [0,0]                       dungeon[m][n]=dungeon[m][n]>=0?0:-dungeon[m][n];               } else if(m==dungeon.length-1){                                  //AT THE RIGHT EDGE                       dungeon[m][n]=dungeon[m][n+1]-dungeon[m][n]>0?dungeon[m][n+1]-dungeon[m][n]:0;               } else if(n==dungeon[0].length-1) {                              //AT THE BOTTOM EGDE                       dungeon[m][n]=(dungeon[m+1][n]-dungeon[m][n])>0?dungeon[m+1][n]-dungeon[m][n]:0;               } else {                                                         //IN A ROOM WITH A PATH TO BE DECIDED                   if(dungeon[m+1][n]
0?dungeon[m+1][n]-dungeon[m][n]:0; } else { dungeon[m][n]=(dungeon[m][n+1]-dungeon[m][n])>0?dungeon[m][n+1]-dungeon[m][n]:0; } } } return dungeon[0][0]+1; }}

转载地址:http://dpuni.baihongyu.com/

你可能感兴趣的文章
IOS开发的开源库
查看>>
Jenkins - sonarqube 代码审查
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成(一)
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成 - 单机部署(二)
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成 - 高可用集群部署(三)
查看>>
Linux 粘滞位 suid sgid
查看>>
C#控件集DotNetBar安装及破解
查看>>
Winform皮肤控件IrisSkin4.dll使用
查看>>
Winform多线程
查看>>
C# 托管与非托管
查看>>
Node.js中的事件驱动编程详解
查看>>
mongodb管理与安全认证
查看>>
nodejs内存控制
查看>>
nodejs Stream使用中的陷阱
查看>>
MongoDB 数据文件备份与恢复
查看>>
MongoDB数据库插入、更新和删除操作详解
查看>>
MongoDB文档(Document)全局唯一ID的设计思路
查看>>
mongoDB简介
查看>>
Redis持久化存储(AOF与RDB两种模式)
查看>>
memcached工作原理与优化建议
查看>>