国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

LeetCode -- Dungeon Game

2019-11-08 02:11:28
字體:
供稿:網(wǎng)友
題目描述: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,從左上開始,狀態(tài)轉(zhuǎn)移無非是向右或向下(邊界值允許的情況下):Dfs (row + 1, col);Dfs (row , col + 1);不夠高效,會導致運行超時。實現(xiàn)代碼:
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]為解。實現(xiàn)代碼:
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);    }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 会宁县| 靖安县| 石屏县| 五莲县| 普陀区| 漳浦县| 沅陵县| 灵丘县| 建始县| 松江区| 永济市| 瑞安市| 十堰市| 梅州市| 九龙坡区| 板桥市| 维西| 宁阳县| 乐平市| 连城县| 右玉县| 炉霍县| 定襄县| 正安县| 赣榆县| 米易县| 穆棱市| 枝江市| 西林县| 洛隆县| 密云县| 黎城县| 松原市| 基隆市| 龙里县| 荆门市| 丹江口市| 云阳县| 库尔勒市| 肇源县| 岢岚县|