Unique Paths

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[i][j]表示到matrix[i][j]的路径个数。则dp[i][j] = dp[i-1][j] + dp[i][j-1]。

int uniquePaths(int m, int n) {
if(m <=0 || n <= 0)return 0;
vector<vector<int> > dp(m);
int i,j;
for(i=0;i<m;i++)
{
vector<int> tmp(n,1);//至少一条
dp[i] = tmp;
}
for(i = 1;i < m;i++)
{
for(j = 1;j < n;j++)
{
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}

Unique Paths II

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.

Note: m and n will be at most 100.

思路:和上面类似,仅仅是当obstacleGrid[i][j] == 0时要把dp[i][j]=0。表示此路不通,初始化时也要注意

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int rows = obstacleGrid.size();
if(rows <= 0)return 0;
int cols = obstacleGrid[0].size();
if(cols <= 0)return 0;
vector<vector<int> > dp(rows);
int i,j;
for(i = 0;i < rows;i++)
{
vector<int> tmp(cols);
dp[i] = tmp;
}
dp[0][0] = obstacleGrid[0][0] == 0 ? 1 : 0;
for(i = 1;i < rows;i++)dp[i][0] = obstacleGrid[i][0] == 0 ? dp[i-1][0] : 0;//当为0时,不能简单的初始化为1。要初始化为前面的值,由于可能被前面挡住了
for(j = 1;j < cols;j++)dp[0][j] = obstacleGrid[0][j] == 0 ? dp[0][j-1] : 0;
for(i = 1;i < rows;i++)
{
for(j = 1;j < cols;j++)
{
dp[i][j] = obstacleGrid[i][j] == 0 ? dp[i-1][j] + dp[i][j-1] : 0;
}
}
return dp[rows-1][cols-1];
}
};

版权声明:本文博客原创文章,博客,未经同意,不得转载。

leetcode 之 Unique Paths的更多相关文章

  1. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

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

  2. [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 ...

  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] 63. Unique Paths II_ Medium tag: Dynamic Programming

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

  5. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

  6. [Leetcode Week12]Unique Paths

    Unique Paths 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths/description/ Description A ...

  7. [LeetCode] 63. Unique Paths II 不同的路径之二

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

  8. [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 ...

  9. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  10. 【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 ...

随机推荐

  1. Javascript中的__proto__、prototype、constructor

    今天重温了下Javacript,给大家带来一篇Javascript博文,相信对于Javacript有一定了解的人都听过prototype原型这个概念,今天我们深度的分析下prototype与__pro ...

  2. CenOS下安装jdk

    1. 安装JDK1.7.0 下载完成后在取得root权限后执行: [root@sea sea]# sudo rpm -ivh /目录/jdk-7-linux-x64.rpm 执行结果: Prepari ...

  3. rhel5.8 ISO yum源配置

    [root@lei1 mnt]# mkdir /mnt/iso [root@lei1 mnt]# mkdir /mnt/cdrom [root@lei1 ~]# mv rhel-server-5.8- ...

  4. Java 大数类

    划分结果存在数组.供应商下标0 在剩下的标记1 import java.math.BigInteger; import java.util.Scanner; public class Main { p ...

  5. Oracle的海量存储技术

    下午去參加一个Oracle有关海量数据存储技术的培训讲座了. 地址在广州市林和西路101号天河区计经大楼西側三楼. 培训发起机构为:广州中睿信息技术有限公司. 以下就简要总结一下所听到的一些东西,也算 ...

  6. UVa 572 Oil Deposits(DFS)

     Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil ...

  7. eclipse在maven项目交付svn忽略简介

    文章来源:http://blog.csdn.net/chaijunkun/article/details/34805385,转载请注明. 不时因为它将有关鲍恩梳理,它会做出相应的内容不变.文. ecl ...

  8. GotoTest

    循环标签跳出循环 public class GotoTest { public static void main(String[] args) { int[][] array={ {1,20,3}, ...

  9. SQLSERVER PRINT语句的换行

    原文:SQLSERVER PRINT语句的换行 SQLSERVER  PRINT语句的换行 想在输出的PRINT语句里面换行,可以这样做 /* SQL的换行 制表符 CHAR(9) 换行符 CHAR( ...

  10. c++ 学籍管理系统v 1.0

    #include<iostream> #include <string> #include<conio.h> using namespace std; class ...