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.

class Solution {
public:
int uniquePaths(int m, int n) {
int path[m][n];
for(int i=;i<m;++i)
path[i][]=;
for(int i=;i<n;++i)
path[][i]=;
for(int i=;i<m;++i)
{
for(int j=;j<n;++j)
{
path[i][j]=path[i-][j]+path[i][j-];
}
}
return path[m-][n-];
}
};

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 as1and0respectively 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 is2.

Note: m and n will be at most 100.

有障碍物的地方不能走,循环内加判断

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int m=obstacleGrid.size();
int n=obstacleGrid[].size();
int path[m][n];
for(int i=;i<m;++i)
{
for(int j=;j<n;++j)
{
if(obstacleGrid[i][j]==)
{
path[i][j]=;
continue;
}
if(i==&&j==){
path[i][j]=;
continue;
}
if(i==)
{
path[i][j]=path[i][j-];
} else if(j==){
path[i][j]=path[i-][j];
}
else{
path[i][j]=path[i-][j]+path[i][j-];
}
}
}
return path[m-][n-];
}
};

unique-paths I &II 路径数,动态规划的更多相关文章

  1. 【LeetCode每天一题】Unique Paths(唯一的路径数)

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

  2. [LeetCode] Unique Paths 不同的路径

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

  3. [LeetCode] 62. Unique Paths 不同的路径

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

  4. LeetCode:Unique Paths I II

    Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...

  5. LeetCode: Unique Paths I & II & Minimum Path Sum

    Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m  ...

  6. LeetCode OJ:Unique Paths(唯一路径)

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

  7. Unique Paths I,II

    题目来自于:https://leetcode.com/problems/unique-paths/ :https://leetcode.com/problems/unique-paths-ii/ A ...

  8. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

  9. 63. Unique Paths II(有障碍的路径 动态规划)

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

随机推荐

  1. 《iOS 7 应用开发实战详解》

    <iOS 7 应用开发实战详解> 基本信息 作者: 朱元波    管蕾 出版社:人民邮电出版社 ISBN:9787115343697 上架时间:2014-4-25 出版日期:2014 年5 ...

  2. .Net Core配置文件读取整理

    一 .配置文件说明 1.配置,主要是 指在程序中使用的一些特殊参数,并且大多数 仅在程序启动的之后指定不需要修改. 2.在以前.Net项目中配置文件主要指app.config或web.config,但 ...

  3. Asp.Net 管道事件注册/HttpApplication事件注册

    一.HttpApplication简介 在HttpRuntime创建了HttpContext对象之后,HttpRuntime将随后创建一个用于处理请求的对象,这个对象的类型为HttpApplicati ...

  4. [转]php rsa加密解密实例

    转自:http://blog.csdn.net/clh604/article/details/20224735 php服务端与客户端交互.提供开放api时,通常需要对敏感的部分api数据传输进行数据加 ...

  5. iOS:在tableView中通过Masonry使用autolayout在iOS7系统出现约束崩溃

    一.出现崩溃情景: 给tableView创建一个头视图,也即tableHeaderView,然后使用Masonry并切换到iOS7/7.1系统给tableHeaderView中的所有子视图添加约束,此 ...

  6. 第三章 Typescript 基本数据类型

    Typescript 基本数据类型 一.基本数据类型 Boolean Number String Array Tuple Enum Any Void Null 和 Undefined Never 二. ...

  7. tensorflow语义分割api使用(deeplab训练cityscapes)

    安装教程:https://github.com/tensorflow/models/blob/master/research/deeplab/g3doc/installation.md citysca ...

  8. 如何让我domain里的机器都跟domain controller的时间保持一致?

    貌似是应该先在PDC上设一个时间源服务器, 然后, 再让domain里所有的机器都去与PDC去sync时间即可. 可是笔者的环境里, 怎么都配不同, 我觉得可能是实验室的网络有什么特别的设置吧. 不管 ...

  9. Spring Boot集成JasperReports生成PDF文档

    由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲染生成PDF文档.本人文采欠缺,写作能力 ...

  10. 【python3】集合set (转)

    https://www.cnblogs.com/onepeace/p/4791578.html set原理 Python 还 包 含 了 一 个 数 据 类 型—— set ( 集 合 ) . 集 合 ...