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. 选中TreeView的某节点,并加背景颜色

    一:按钮事件,遍历所有节点 private void button2_Click(object sender, EventArgs e) { foreach (TreeNode n in TreeVi ...

  2. Mysql 编译安装并使用自定义用户启动

    本文基于 Redhat Linux 6.7 的环境,Mysql 版本为 5.5.37 安装前的检查 必备的组件,如果没有使用 yum 进行安装,可以使用网上的源,也可以使用本地光盘作为 Yum 源. ...

  3. [转]你如何面对—LNMP高并发时502

    From : http://www.topthink.com/topic/5683.html 之前php-fpm配置: 单个php-fpm实例,使用socket方式,内存8G 静态方式,启动php-f ...

  4. [转]PHP之APC缓存详细介绍(学习整理)

    From : http://www.2cto.com/kf/201210/160140.html 1.APC缓存简介APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存 ...

  5. 【BZOJ】【3673】可持久化并查集 & 【3674】可持久化并查集加强版

    可持久化并查集 Orz hzwer & zyf 呃学习了一下可持久化并查集的姿势……其实并查集就是一个fa数组(可能还要带一个size或rank数组),那么我们对并查集可持久化其实就是实现一个 ...

  6. 混沌分形之逻辑斯蒂(Logistic)映射系统

    前几天,有个同事看到我生成的一幅逻辑斯蒂分岔图像后,问我:“这是咪咪吗?”我回答:“淫者见淫.”好吧,这里将生成几种分岔映射图形,包括逻辑斯蒂映射系统,正弦映射系统和曼德勃罗映射系统.实际上这几种图形 ...

  7. tmux 终端分屏利器使用

    介绍 Tmux 是一个工具,用于在一个终端窗口中运行多个终端会话. 不仅如此,你还可以通过 Tmux 使终端会话运行于后台或是按需接入.断开会话,这个功能非常实用. 很好的工具,记录一下,以后要常用. ...

  8. Java中 CopyOnWriteArrayList 的使用

    java中,List在遍历的时候,如果被修改了会抛出java.util.ConcurrentModificationException错误. 看如下代码: import java.util.Array ...

  9. [leetcode]Word Break @ Python

    原题地址:https://oj.leetcode.com/problems/word-break/ 题意: Given a string s and a dictionary of words dic ...

  10. ZooKeeper 了解