题目链接

题目大意:从左上角到右下角,每一个格子都有各自的权值,如果权值为负,则当到达时,要失血;如果权值为正,则当到达时,要加血。当到达某个格子时,当前血量<=0,则死亡,到达不了右下角,所以此时要计算从左上角到右下角,初始应该最少携带多少血(即经过所有路径后所计算出的值),才不会死亡,能正常到达右下角。

法一:dfs,模板深搜,果断超时。要注意:更新点不是整条路径的和(与64题比较)的最小值,而是整条路径中所达到的最大的失血量,如果最大失血量>=0,则本身只需要携带1个血即可;否则本身携带的血应该=abs(最大失血量)+1。代码如下:

     public int calculatedMinimumHP(int[][] dungeon) {
boolean vis[][] = new boolean[dungeon.length][dungeon[0].length];
int f[][] = {{1, 0}, {0, 1}};
vis[0][0] = false;
return dfs(dungeon, 0, 0, dungeon[0][0], Integer.MAX_VALUE, dungeon[0][0], vis, f);
}
public int dfs(int[][] dungeon, int x, int y, int sum, int res, int blood, boolean vis[][], int f[][]) {
if(x == dungeon.length - 1 && y == dungeon[0].length - 1) {
//递归结束点是每条线路的过程中的最大失血量
if(blood < 0) {//如果失血量为负数,则是绝对值+1
if(Math.abs(blood) < res) {
res = Math.abs(blood) + 1;
}
}
else {//如果失血量>=0,则是1,因为当失血量为0时,也会死亡
res = 1;
}
return res;
}
for(int i = 0; i < 2; i++) {
int cnt_x = x + f[i][0];
int cnt_y = y + f[i][1];
if(cnt_x < dungeon.length && cnt_y < dungeon[0].length && vis[cnt_x][cnt_y] == false) {
vis[cnt_x][cnt_y] = true;
res = dfs(dungeon, cnt_x, cnt_y, sum + dungeon[cnt_x][cnt_y], res, blood < (sum + dungeon[cnt_x][cnt_y]) ? blood : (sum + dungeon[cnt_x][cnt_y]), vis, f);
vis[cnt_x][cnt_y] = false;
}
}
return res;
}

法二(借鉴):dp,类似于64的二维dp,64是从左上往右下,而这题是从右下往左上。dp[i][j]表示从坐标[i,j]到右下角的路径中需要的最少血量,这样每次计算时,都可以用到下面和右面的dp值,从中取最小再将当前值减去即可。其中要追的地方与dfs相似,在每一个格子中,都要始终保持至少1个血量。dp公式:dp[i][j] = min(dp[i + 1][j], dp[i][j + 1]) - du[i][j]。代码如下(耗时2ms):

     public int calculatedMinimumHP(int[][] dungeon) {
int dp[][] = new int[dungeon.length][dungeon[0].length];
//初始化最后一列
//由于走到每个格子时,都要保持至少一个血量,所以应该用到max(1, ...)
dp[dungeon.length - 1][dungeon[0].length - 1] = Math.max(1, 1 - dungeon[dungeon.length - 1][dungeon[0].length - 1]);
for(int i = dungeon.length - 2; i >= 0; i--) {
dp[i][dungeon[0].length - 1] = Math.max(1, dp[i + 1][dungeon[0].length - 1] - dungeon[i][dungeon[0].length - 1]);
}
//初始化最后一行
for(int i = dungeon[0].length - 2; i >= 0; i--) {
dp[dungeon.length - 1][i] = Math.max(1, dp[dungeon.length - 1][i + 1] - dungeon[dungeon.length - 1][i]);
}
//计算dp
for(int i = dungeon.length - 2; i >= 0; i--) {
for(int j = dungeon[0].length - 2; j >= 0; j--) {
//从下边和右边中取出最小者,然后减去当前值
dp[i][j] = Math.max(1, Math.min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]);
}
}
return dp[0][0];
}

174.Dungeon Game---dp的更多相关文章

  1. Java for LeetCode 174 Dungeon Game

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  2. leetcode@ [174] Dungeon Game (Dynamic Programming)

    https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...

  3. [LeetCode] 174. Dungeon Game 地牢游戏

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  4. ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  5. 174. Dungeon Game

    题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...

  6. 174. Dungeon Game(动态规划)

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  7. [leetcode]174. Dungeon Game地牢游戏

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  8. LeetCode 174. Dungeon Game (C++)

    题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...

  9. 174 Dungeon Game 地下城游戏

    一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格布局.我们英勇的骑士(K)最初被安置在左上角的房间里,并且必须通过地下城对抗来拯救公主.骑士具有以正整数 ...

随机推荐

  1. 【bzoj3064】Tyvj 1518 CPU监控 线段树维护历史最值

    题目描述 给你一个序列,支持4种操作:1.查询区间最大值:2.查询区间历史最大值:3.区间加:4.区间赋值. 输入 第一行一个正整数T,表示Bob需要监视CPU的总时间. 然后第二行给出T个数表示在你 ...

  2. Argus UVALive - 3135(优先队列 水题一道)

    有一系列的事件,它每Period秒钟就会产生编号为qNum的事件,你的任务是模拟出前k个事件,如果多个事件同时发生,先处理qNum小的事件 今天再看看数据结构.. #include <iostr ...

  3. Cells UVALive - 3486(dfs序+手动开栈)

    给一棵树,每次每次询问一个点是否是另一个点的祖先? 输入时是每个下标对应节点的儿子的数量 用dfs序 时间戳.. 如果一个点是另一个点的祖先,那么它的两个标记一定在祖先的范围之内 #include & ...

  4. 笔记-自己看Day20-待续

    1. 搭建环境 1)注释掉csrf 2)配置模板路径 'DIRS': [os.path.join(BASE_DIR,'templates')],  # BASE_DIR,代指当前目录. 3)配置静态文 ...

  5. WPA-PSK无线网络破解原理及过程

    原文链接地址:http://www.freebuf.com/articles/wireless/58342.html 本文将主要讲讲WPA-PSK类型的无线网络安全问题,首先我们看下802.11协议相 ...

  6. MapReduce(四) 典型编程场景(二)

    一.MapJoin-DistributedCache 应用 1.mapreduce join 介绍 在各种实际业务场景中,按照某个关键字对两份数据进行连接是非常常见的.如果两份数据 都比较小,那么可以 ...

  7. NOIP2017 Day2 T3 列队(treap)

    可以直接用treap上大模拟...n+1个treap维护n行的前m-1个点和最后一列. 需要支持删除一个点或者一段区间,而空间并不支持存下所有的点的时候,可以用一个点代替一个区间,记录区间首项的值和区 ...

  8. mysql三-2:数据类型

    一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考: http://www.runoob.com/mysql/mysql-data ...

  9. [树莓派]wifi在面板看不到,但是可以scan到的解决方案

    突然遇到一个问题,发现在wifi面板中找不到WiFi,但是运行scan命令可以发现WiFi,多方查找资料终于找到了一个解决方案: 运行如下命令: sudo apt-get install wicd   ...

  10. powerdesigner中物理模型与sql脚本的以及与数据库的连接设置

    使用JDBC连接失败的解决方案: http://blog.csdn.net/t37240/article/details/51595097 使用powerdesigner工具我们可以方便的根据需求分析 ...