当前位置: 首页 > news >正文

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


一个矩阵游戏。骑士从左上角走到右下角,过程中会遇到怪物或加血,以正负值表示。求骑士到达右下角所需的最小血量。


本题最直接的思路是DFS,从左上开始,状态转移无非是向右或向下(边界值允许的情况下):
Dfs (row + 1, col);
Dfs (row , col + 1);


不够高效,会导致运行超时。
实现代码:
public class Solution {
    private List<int> _result;
    private int _row;
    private int _col;
    public int CalculateMinimumHP(int[,] dungeon) 
    {
    	_result = new List<int>();
    	_row = dungeon.GetLength(0);
    	_col = dungeon.GetLength(1);
    	
    	Dfs(dungeon, 0,0,0,0);
    	var min = _result.Min();
    	return min;
    }
    
    public void Dfs(int[,] game, int r, int c, int minHp, int hp)
    {
    	hp += game[r,c];
    	if(hp <= 0 && hp < minHp){
    		minHp = hp ;
    	}
    	
    	// check ending 
    	if((r == _row-1) && (c == _col-1)){
    		if(minHp < 0){
    			_result.Add(1-minHp);
    		}else{
    			_result.Add(1);
    		}
    		
    		return ;
    	}
    	if (r < _row - 1){
    		Dfs(game, r + 1, c, minHp, hp);
    	}
    	if (c < _col - 1){
    		Dfs(game, r, c + 1, minHp, hp);
    	}
    }


}






解法二:


使用dp。从右下角考虑所需最小血量,minHp[row-1,col-1] = Math.Max(1-dungeon[row-1,col-1],1);
初始化边界值。
向上走或向左走,取最小值:
dp[i,j] = Min(dp[i+1,j]-dungeon[i+1,j], dp[i,j+1]-dungeon[i,j])
dp[0,0]为解。


实现代码:


public class Solution {
   
    public int CalculateMinimumHP(int[,] dungeon) 
    {
    	var row = dungeon.GetLength(0);
    	var col = dungeon.GetLength(1);
    	
    	// the min hp to have
    	var dp = new int[row, col];
    	dp [row-1, col-1] = MinHp(1- dungeon[row-1,col-1]);
    	
    	// set last row
    	for (var i = col - 2;i >= 0; i--){
    		dp[row-1,i] = MinHp(dp[row-1,i+1] - dungeon[row-1,i]);
    	}
    	
    	// set last col
    	for (var i = row - 2;i >= 0; i--){
    		dp[i,col-1] = MinHp(dp[i+1,col-1] - dungeon[i,col-1]);
    	}
    	
    	for (var i = row-2;i >= 0;i --){
    		for (var j = col - 2; j >= 0; j--){
    			// come down or right
    			dp[i,j] = Math.Min(MinHp(dp[i+1,j] - dungeon[i,j]),MinHp(dp[i,j+1] -dungeon[i,j]));
    		}
    	}
    	
    	return dp[0,0];
    }
    
    
    private int MinHp(int x){
    	return  Math.Max(x, 1);
    }




}


相关文章:

  • 从Oracle到DB2,问题集(二)
  • LeetCode -- Contains Duplicate II
  • Sql union的反义词Minus
  • LeetCode -- Path Sum III
  • LeetCode -- Minimum Number of Arrows to Burst Balloons
  • 反醒反醒
  • LeetCode -- Arranging Coins
  • Bing在中国不会成功
  • LeetCode -- First Unique Character in a String
  • 搜狗输入法,无心插柳柳成荫
  • LeetCode -- Wildcard Matching
  • 弥平“第三道鸿沟”:3G运营商必须承担的社会责任
  • 使用面向对象重构之-从过程式设计到面向对象
  • Bing API初体验
  • 使用面向对象重构之-继承中的抽象—模板方法
  • python3.6+scrapy+mysql 爬虫实战
  • 【347天】每日项目总结系列085(2018.01.18)
  • Angular数据绑定机制
  • Django 博客开发教程 16 - 统计文章阅读量
  • Effective Java 笔记(一)
  • ES6 ...操作符
  • JS创建对象模式及其对象原型链探究(一):Object模式
  • Leetcode 27 Remove Element
  • MaxCompute访问TableStore(OTS) 数据
  • tab.js分享及浏览器兼容性问题汇总
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 对超线程几个不同角度的解释
  • 多线程事务回滚
  • 关于Java中分层中遇到的一些问题
  • 机器人定位导航技术 激光SLAM与视觉SLAM谁更胜一筹?
  • 基于组件的设计工作流与界面抽象
  • 技术:超级实用的电脑小技巧
  • 手写双向链表LinkedList的几个常用功能
  • 一道面试题引发的“血案”
  • 智能合约开发环境搭建及Hello World合约
  • 仓管云——企业云erp功能有哪些?
  • 容器镜像
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • ​Python 3 新特性:类型注解
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • # 达梦数据库知识点
  • (+4)2.2UML建模图
  • (10)ATF MMU转换表
  • (3)STL算法之搜索
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转)C#开发微信门户及应用(1)--开始使用微信接口
  • (转)Linq学习笔记
  • (转)memcache、redis缓存
  • .form文件_SSM框架文件上传篇
  • .NET 2.0中新增的一些TryGet,TryParse等方法