题目链接

题目大意:与62题类似,只是这个题中间有障碍。

法一:dfs,依旧超时。代码如下:

     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
boolean vis[][] = new boolean[obstacleGrid.length][obstacleGrid[0].length];
int f[][] = {{1, 0}, {0, 1}};
//如果起始格子就是障碍,则表示不能正常到达目的地
if(obstacleGrid[0][0] == 1) {
return 0;
}
return dfs(obstacleGrid, 0, 0, 0, f, vis);
}
public static int dfs(int[][] obstacleGrid, int x, int y, int cnt, int f[][], boolean vis[][]) {
if(x == obstacleGrid.length - 1 && y == obstacleGrid[0].length - 1 && obstacleGrid[x][y] == 0) {
cnt++;
return cnt;
}
for(int i = 0; i < 2; i++) {
int cnt_x = x + f[i][0];
int cnt_y = y + f[i][1];
if(cnt_x < obstacleGrid.length && cnt_y < obstacleGrid[0].length && vis[cnt_x][cnt_y] == false && obstacleGrid[cnt_x][cnt_y] == 0) {
vis[cnt_x][cnt_y] = true;
cnt = dfs(obstacleGrid, cnt_x, cnt_y, cnt, f, vis);
vis[cnt_x][cnt_y] = false;
}
}
return cnt;
}

法二:dp,模仿62题的dp,只是这里要考虑障碍。代码如下(耗时2ms):

     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
//如果起始位置或结束位置是1,则直接不通
if(obstacleGrid[0][0] == 1 || obstacleGrid[obstacleGrid.length - 1][obstacleGrid[0].length - 1] == 1) {
return 0;
}
int dp[][] = new int[obstacleGrid.length][obstacleGrid[0].length];
//初始化第一列
for(int i = 0; i < obstacleGrid.length; i++) {
if(obstacleGrid[i][0] == 0) {
dp[i][0] = 1;
}
else {//第一列,一旦碰到一个障碍1,则第一列障碍下面的所有都是障碍,不通
for(; i < obstacleGrid.length; i++) {
dp[i][0] = 0;
}
break;
}
}
//初始化第一行
for(int i = 0; i < obstacleGrid[0].length; i++) {
if(obstacleGrid[0][i] == 0) {
dp[0][i] = 1;
}
else {//第一行,一旦碰到一个障碍1,则第一行障碍后面的所有都是障碍,不通
for(; i < obstacleGrid[0].length; i++) {
dp[0][i] = 0;
}
break;
}
}
//计算dp
for(int i = 1; i < obstacleGrid.length; i++) {
for(int j = 1; j < obstacleGrid[0].length; j++) {
if(obstacleGrid[i][j] == 1) {//如果当前格是障碍,则不可通
dp[i][j] = 0;
}
else {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
return dp[obstacleGrid.length - 1][obstacleGrid[0].length - 1];
}

法三:一维dp,与62题很类似,但是要比62题难思考一点,因为要考虑障碍的问题,而且没有初始化,直接从0开始遍历的。代码如下(耗时1ms):

     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid[0][0] == 1 || obstacleGrid[obstacleGrid.length - 1][obstacleGrid[0].length - 1] == 1) {
return 0;
}
int[] dp = new int[obstacleGrid[0].length];
dp[0] = 1;
//从0开始遍历,否则会漏掉第一列,因为其实第一列并没有初始化
for(int i = 0; i < obstacleGrid.length; i++) {
for(int j = 0; j < obstacleGrid[0].length; j++) {
//遇到障碍则赋0
if(obstacleGrid[i][j] == 1) {
dp[j] = 0;
}
//由于j是从0开始,所以只考虑>0的情况
else if(j > 0){
dp[j] += dp[j - 1];
}
}
}
return dp[obstacleGrid[0].length - 1];
}

63.UniquePaths II---dp的更多相关文章

  1. LeetCode-Palindrome Partitioning II[dp]

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  2. hdu 1207 汉诺塔II (DP+递推)

    汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. #1560 : H国的身份证号码II(dp+矩阵快速幂)

    #1560 : H国的身份证号码II 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 H国的身份证号码是一个N位的正整数(首位不能是0).此外,由于防伪需要,一个N位正整 ...

  4. poj-2336 Ferry Loading II(dp)

    题目链接: Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3946   Accepted: ...

  5. HDOJ 5087 Revenge of LIS II DP

    DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  6. hihocoder 1828 Saving Tang Monk II (DP+BFS)

    题目链接 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great C ...

  7. hdu5087 Revenge of LIS II (dp)

    只要理解了LIS,这道题稍微搞一下就行了. 求LIS(最长上升子序列)有两种方法: 1.O(n^2)的算法:设dp[i]为以a[i]结尾的最长上升子序列的长度.dp[i]最少也得是1,就初始化为1,则 ...

  8. [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp

    4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec  Memory Limit: 128 MBSubmi ...

  9. leetcode-63. Unique Paths II · DP + vector

    题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

随机推荐

  1. TCP的拥塞控制 (四)

     TCP NewReno   NewReno是在Reno的基础上,改进了Fast Recovery,主要思想是保证处于network中的packet的总量是饱和的. 在Reno算法中,一个超时会导致相 ...

  2. BZOJ3262:陌上花开 & 洛谷3810:三维偏序——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3262 https://www.luogu.org/problemnew/show/3810 Desc ...

  3. 洛谷 P2900 [USACO08MAR]土地征用Land Acquisition 解题报告

    P2900 [USACO08MAR]土地征用Land Acquisition 题目描述 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地.如果约翰单买一块土 地,价格就是土地的面积.但他可以选 ...

  4. 延长xss的攻击(转)

    XSS 的本质仍是一段脚本.和其他文档元素一样,页面关了一切都销毁.除非能将脚本蔓延到页面以外的地方,那样才能获得更长的生命力. 庆幸的是,从 DOM 诞生的那一天起,就已为我们准备了这个特殊的功能, ...

  5. Hadoop1重新格式化HDFS

    注意:原来的数据全部被清空了.产生了一个新的hdfs 1. 删除已有目录 1.1 查看hdfs-site.xml 将 dfs.name.dir和dfs.data.dir所指定的目录删除 vim hdf ...

  6. Linux基础-shell脚本知识整理和脚本编写----------变量、运算符、流程控制、函数、计划任务(发送邮件)

    I:知识整理:变量.运算符.流程控制.函数.计划任务 变量 系统变量:set:显示所有变量                env:环境变量 常用系统变量: path pwd lang home his ...

  7. JS中如何使用EL表达式中的对象

    JS中如何使用EL表达式中的对象 2017年09月25日 15:33:09 lhpnba 阅读数:4859   1.js中使用el表达式要加双引号或单引号:'${list}' 2.js变量获取el表达 ...

  8. JSP2 特性

    JSP2 新特性 1.直接配置 JSP 属性 2.表达式语言 3.简化的自定义标签 API 4.Tag 文件语法 如果要使用 JSP2 语法,web.xml 文件必须使用 Servlet2.4 以上版 ...

  9. java中Mysql开发

    [IntelliJ IDEA 12使用]导入外部包 http://www.cnblogs.com/haochuang/p/3491959.html JDBC导入包即可 http://blog.163. ...

  10. C++单例模式设计与实现

    C++单例模式主要用途就是整个程序中只实例化一个对象,之后获取到的都是该对象本身进行处理问题. 单例模式一般都是在函数中采用局部静态变量完成的,因为局部的静态变量生命周期是随着程序的生命周期 一起结束 ...