题目链接

题目大意:与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. Java NIO中的Buffer

    简介 Buffer缓冲区,首先要弄明白的是,缓冲区是怎样一个概念.它其实是缓存的一种,我们常说的缓存,包括保存在硬盘上的浏览器缓存,保存在内存中的缓存(比如Redis.memcached).Buffe ...

  2. 【JavaScript&jQuery】返回顶部

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 《Linux内核设计与实现》读书笔记——第一二章

    <Linux内核设计与实现>读书笔记——第一二章 第一章 Linux内核简介 1.1 Unix的历史 简洁:仅提供系统调用并有一个非常明确的设计目的. 抽象:Unix中绝大部分东西都被当做 ...

  4. Oracle中rank() over, dense_rank(), row_number() 的区别

    摘自:http://www.linuxidc.com/Linux/2015-04/116349.htm Oracle 中 rank() over, dense_rank(), row_number() ...

  5. oracle-DECODE()函数

    DECODE()函数 DECODE(value, if1, then1, if2,then2, if3,then3, . . . else ) 含义解释: DECODE(条件,值1,翻译值1,值2,翻 ...

  6. 《javascript高级程序设计(第3版)》-1

    javascript有下列三个不同的部分组成: ECMAScript,由ECMA-262定义,提供核心语言功能 文档对象模型(DOM),提供访问和操作网页内容的方法和接口 浏览器对象模型(BOM),提 ...

  7. HDU--2962

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 分析:最短路+二分. #include<iostream> #include< ...

  8. ural 1297 后缀数组 最长回文子串

    https://vjudge.net/problem/URAL-1297 题意: 给出一个字符串求最长回文子串 代码: //论文题,把字符串反过来复制一遍到后边,中间用一个没出现的字符隔开,然后就是枚 ...

  9. Java基础之equals() 和 hashCode()

    equals()是Object中的一个方法: public boolean equals(Object obj) { return (this == obj); } 在Object中equals()方 ...

  10. MySQL性能优化之道

    1.in和not in子查询优化 not in 是不能命中索引的,所以以下子查询性能很低. 如果是确定且有限的集合时,可以使用.如 IN (0,1,2). 用 exists或 notexists代替 ...