【 声明:版权全部,转载请标明出处,请勿用于商业用途。  联系信箱:libin493073668@sina.com】



题意:
给定一个二维矩阵,当中0代表这个位置能够走。1代表这个位置不能走,还是从(1,1)走到(n,m)。问有多少种走法

思路:
dp[i][j]代表走到(i,j)有多少种走法
因为(i,j)仅仅能从(i-1,j)与(i,j-1)走到,所以状态转移方程为:
dp[i][j]=dp[i-1][j]+dp[i][j-1];
特定的,对于此格为1的情况。由于这一个走不到,所以此时dp[i][j]=0

class Solution
{
public:
int uniquePathsWithObstacles(vector<vector<int> >& a)
{
int i,j;
int n = a.size(),m = a[0].size();
vector<vector<int> > dp;
dp.resize(n+1);
for(i = 0; i<=n; i++)
dp[i].resize(m+1);
dp[0][0] = !a[0][0];
for(i = 0; i<n; i++)
{
for(j = 0; j<m; j++)
{
if(i==0&&j==0) continue;
if(a[i][j]==1)
{
dp[i][j] = 0;
continue;
}
if(i == 0)
{
dp[i][j]=dp[i][j-1];
continue;
}
if(j == 0)
{
dp[i][j]=dp[i-1][j];
continue;
}
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
return dp[n-1][m-1];
}
};

[LeedCode OJ]#63 Unique Paths II的更多相关文章

  1. LeetCode OJ 63. Unique Paths II

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

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

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

  3. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  4. 【LeetCode】63. Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

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

  6. leetcode 63. Unique Paths II

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

  7. 63. Unique Paths II

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

  8. 63. Unique Paths II(中等, 能独立做出来的DP类第二个题^^)

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

  9. 【一天一道LeetCode】#63. Unique Paths II

    一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...

随机推荐

  1. CAD得到ImageMark数据(com接口VB语言)

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  2. ThinkPHP---案例--实现知识管理功能

    [一]准备工作 (1)数据表sp_knowledge SQL语句:知识管理数据表结构 create table sp_knowledge( id int(11) not null auto_incre ...

  3. 00Enterprise Resource Planning

    Enterprise Resource Planning         企业资源计划即 ERP (Enterprise Resource Planning),由美国 Gartner Group 公司 ...

  4. PHP 浏览器端输出中文正常,cmd中文乱码

    cmd 运行php脚本乱码问题如果别的编码根据下面的自己换吧!chcp 65001 就是换成UTF-8chcp 936 可以换回默认的GBKchcp 437 是美国英语

  5. gym101673G. A Question of Ingestion (DP)

    题意:有最多100天 每天有一个食物量 你一开始有一个最大胃口表示你最开始能吃多少食物 如果你昨天吃了 那么今天的胃口为昨天的2/3 如果你前天吃了 昨天没吃 那么你的胃口可以恢复到前天的情况 如果你 ...

  6. idea使用maven install命令打包(springboot),jar运行时出现没有主清单属性

    原因是:我的项目里除了springboot启动类还自定义了多个main来搞了点小demo,就因为这个原因我花了近一天的时间才找清楚原因. 解决方案:找到多余的main方法,注释或删除掉. (下面可以忽 ...

  7. DH密钥交换算法

    DH密钥交换算法:DH的全称为Diffie-Hellman ,该算法可以在需要安全传输的前提下,确定双方的对称密钥,该算法的核心在于双方的私钥没有进入网络传输流程,根据对方的公钥和己方的私钥,可以计算 ...

  8. DemoKit编译过程

    E:\Project_code\EAE\src_rev_24139_A95LYD\Project\DemoKit>make release Checking uITRON - DemoKit r ...

  9. 洛谷 3398 仓鼠找sugar 【模板】判断树上两链有交

    [题解] 题意就是判断树上两条链是否有交.口诀是“判有交,此链有彼祖”.即其中一条链的端点的Lca在另一条链上. 我们设两条链的端点的Lca中深度较大的为L2,对L2与另一条链的两个端点分别求Lca, ...

  10. [bzoj1500][NOI2005 维修数列] (splay区间操作)

    Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目. 第2行包含N个数字,描述初始时的数列. 以下M行,每 ...