题目链接

题目大意:与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. [cogs1065]绿豆蛙的归宿

    1065. [Nescafe19] 绿豆蛙的归宿 [题目描述] 给出一个有向无环的连通图,起点为1终点为N,每条边都有一个长度.绿豆蛙从起点出发,走向终点.到达每一个顶点时,如果有K条离开该点的道路, ...

  2. 贪心(qwq)习题题解

    贪心(qwq)习题题解 SCOI 题解 [ SCOI2016 美味 ] 假设已经确定了前i位,那么答案ans一定属于一个区间. 从高位往低位贪心,每次区间查找是否存在使此位答案为1的值. 比如6位数确 ...

  3. CF335F Buy One, Get One Free 贪心

    题意: \(n\)个物品,每个物品有一个价格,买一个高价格的物品,可以选择免费得到一个价格严格低于这个物品的物品.求得到\(n\)个物品的最小代价. 题解: 神仙贪心-- 题目要求求出最小代价,相当于 ...

  4. 【CF625E】Frog Fights(模拟)

    [CF625E]Frog Fights(模拟) 题面 CF 洛谷 翻译: 有\(n\)只青蛙在一个被分为了\(m\)等分的圆上,对于每份顺时针依次标号. 初始时每只青蛙所在的位置是\(p_i\),速度 ...

  5. linux内核分析 第三周 构造一个简单的Linux系统MenuOS

    一.计算机的三个法宝 存储程序计算机,函数调用堆栈,中断二.操作系统的两把剑:1.中断上下文的切换,保存现场和恢复现场2.进程上下文的切换. 三.linux内核源代码的分析: ·arch/目录保存支持 ...

  6. bzoj4165: 矩阵(堆+hash)

    求第k大用堆维护最值并出堆的时候扩展的经典题... 因为只有正数,所以一个矩阵的权值肯定比它的任意子矩阵的权值大,那么一开始把所有满足条件的最小矩阵加进堆里,弹出的时候上下左右扩展一行加进堆,用has ...

  7. 解题:POI 2013 Taxis

    题面 设当前位置为$pos$,那么可以发现在出租车总部左侧时,每辆车的贡献是$x[i]-(d-pos)$,而在右侧时只有$x[i]>=m-d$的车能够把人送到,那么首先我们要找出最小的满足$x[ ...

  8. mac 的全文搜索

    grep -Rni "view.proptypes.style" *  需要切换到要搜索的目录在运行

  9. JSTL与EL与OGNL

    springMVC使用JSTL与EL表达式: spring MV默认的jsp页面的标签就是JSTL,而struts2默认的是OGNL标签. struts2 使用OGNL与EL表达式:OGNL用stru ...

  10. vmvare彻底删除(转)

    bat脚本 echo off cls echo "flag">>%windir%\system32\test.log if not exist %windir%\sys ...