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

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

How many possible unique paths are there?

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

咋一看,深搜之,直接超时~~
然后发现是可以拿DP做的,思考出来一个DP方程,虽然不是很优化但是一次过了还是很开心的
以下代码展现了一些思考过程:
1、深搜
2、visit标记数组,仔细分析发现不会走过已经走的路
3、dp[i][j]=dp[i-1][j]+dp[i][j-1] 一开始想着倒推,然后一想似乎不能倒推,是从第一步开始走的

class Solution {
public:int dir[2][2]={{1,0},{0,1}};int route=0;int m,n;int uniquePaths(int m, int n) {this->m=m;this->n=n;// vector<vector<char> > visit(m,n);// char visit[m][n];// memset(visit,0,sizeof(visit));// dfs(1,1);// return route;// vector<vector<int> > dp(m,n);int dp[m+2][n+2];memset(dp,0,sizeof(dp));dp[1][1]=1;for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){dp[i][j+1]=dp[i][j]+dp[i-1][j+1];dp[i+1][j]=dp[i][j]+dp[i+1][j-1];}}return dp[m][n];//不能倒推// for(int i=m-1;i>0;i--){//     for(int j=n-1;j>0;j--){//         dp[i][j]=dp[i-1][j]+dp[i][j-1];//     }// }}void dfs(int x,int y){if(x==m && y==n){route++;return;}for(int i=0;i<2;i++){int next_x=x+dir[i][0];int next_y=y+dir[i][1];//只存在越界,不存在反复访问if(next_x>m || next_y>n) continue;// if(visit[next_x][next_y])dfs(next_x,next_y);}}
};

Follow up for “Unique Paths”:

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.

相比较I的话,II只是增加了一点障碍,使得在进入DP的时候需要一些条件。
由于不小心写错了一个条件所以导致debug好久

不过执行的时间似乎有点长,还有待优化:
参考链接:https://discuss.leetcode.com/topic/15267/4ms-o-n-dp-solution-in-c-with-explanations

class Solution {
public:int m,n;vector<vector<int> > obstacleGrid;int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {int m=obstacleGrid.size();if(m==0) return 0;int n=obstacleGrid[0].size();// if(m==1 && n==1) return !obstacleGrid[0][0];if(obstacleGrid[0][0]) return 0;int dp[m+2][n+2];this->m=m;this->n=n;this->obstacleGrid=obstacleGrid;memset(dp,0,sizeof(dp));dp[1][1]=1;for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){if(!reachAble(i,j)) continue;//这一点是可达的if(reachAble(i,j+1)){dp[i][j+1]=dp[i][j]+reachAble(i-1,j+1)*dp[i-1][j+1];}if(reachAble(i+1,j)){dp[i+1][j]=dp[i][j]+reachAble(i+1,j-1)*dp[i+1][j-1];}}}return dp[m][n];}int reachAble(int x,int y){int i=x-1,j=y-1;if(i>=m || i<0 || j>=n || j<0) return 0;return !obstacleGrid[i][j];}
};

LeetCode | Unique Paths I,II相关推荐

  1. leetcode 62, 63, 980. Unique Paths I, II, III | 62, 63, 980. 不同路径 I, II, III(暴力递归->傻缓存->动态规划)

    62. Unique Paths https://leetcode.com/problems/unique-paths/ 注意本题只能向右 / 向上走. DP 问题,经典又熟悉. 暴力递归->傻 ...

  2. LeetCode Unique Paths

    原题链接在这里:https://leetcode.com/problems/unique-paths/ 题目: A robot is located at the top-left corner of ...

  3. [leetcode]Unique Paths II

    >这道题目和上一题基本是一样的.只需要检测现在正在处理的那个单位是不是不可通过的,如果是就直接让这个单元格=0,也就是没有路径可以到右下角就好了. 代码如下: public class Solu ...

  4. [Lintcode]115. Unique Paths II/[Leetcode]63. Unique Paths II

    115. Unique Paths II/63. Unique Paths II 本题难度: Easy/Medium Topic: Dynamic Programming Description Fo ...

  5. 【动态规划】LeetCode 63. Unique Paths II

    LeetCode 63. Unique Paths II Solution1:我的答案 在哪里做过这题? class Solution { public:int uniquePathsWithObst ...

  6. LeetCode 1.Minimum Path Sum 2.Unique Paths I and II

    大家好,我是刘天昊,快到端午节了,今天说两道动态规划的题目(话说动规真的挺难的) 当然这三题是一样的解体思路先看Unique Paths A robot is located at the top-l ...

  7. Unique Paths II leetcode java

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  8. [LeetCode]--63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. [LeetCode][Java] Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

最新文章

  1. undefined reference to android log print,undefined reference to '__android_log_print'
  2. TortoiseSVN使用详细步骤(一)
  3. 在网页中插入百度地图
  4. UML类图关系表示方法
  5. SQLServer数据库收缩相关知识笔记
  6. 华为全面启航计算战略:“鲲鹏+昇腾”双引擎
  7. python用空格隔开每一个字符_python实现将一串字符每两个一组,中间用空格隔开...
  8. 【Java6学习笔记】多线程编程中使用volatile保障原子性
  9. 程序员英文简历范例(前端)
  10. 献给七夕|微生物和您的爱情生活
  11. java生成竖排文字图片_怎么给图片加上竖排文字?
  12. 金士顿U盘量产根据PS2136_UP10_v1.81.00.rar
  13. MacOS上如何将MOV文件转换为MP4
  14. 洛谷3356火星探险问题
  15. PTA数据结构7.1给定一个初始为空的栈和一系列压栈、弹栈操作,请编写程序输出每次弹栈的元素。栈的元素值均为整数。
  16. 2022年烷基化工艺模拟考试题及烷基化工艺模拟考试题库
  17. android vcard解析代码,Android从Intent获取vCard数据
  18. 人脸扫描Canvas动画
  19. sap砍刀-做了sap半年多了,但是一直没有遇到多少问题,今天在网上看到这篇文章,于是copy过来了(对sap的学习者很有用)...
  20. Video Harmonization一些周边代码

热门文章

  1. WIN10状态栏老是重启解决办法
  2. 机器学习两周学习成果
  3. 【企鹅风讯】腾讯WeTest舆情品牌全新升级 #智能引领,洞见未来#
  4. 用“魔法师”调整分区
  5. Flutter中的事件流和手势简析
  6. 单片机 0xff是什么意思
  7. 23位子网掩码是多少_23位子网掩码ip范围
  8. 【caffe】可视化网络的训练过程的loss和accuracy
  9. lims实验室管理系统试用版应该有哪些功能?都在这了
  10. 言简意赅告诉你KMP算法的原理,不管你信不信,我信了