LeetCode(63)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.
分析
带障碍的路径数计算问题,这个题目与上一题的区别之处在于,m*n的矩阵中有部分设置了障碍,当然有障碍的地方不能通过;
虽然设立了障碍,该题目的本质仍然是一个动态规划问题,我们只需要增加判断当前点是否有障碍的代码即可,若有障碍那么此处不能通行,自然f(i,j)=0,对于其他点,依然用上一题目的推导公式即可!
AC代码
//直接用非递归算法求解
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid.empty())
return 0;
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector<vector<int> > ret(m, vector<int>(n, 0));
//矩阵首列
for (int i = 0; i < m; i++)
{
//无障碍,则有一条路径,否则不通
if (obstacleGrid[i][0] != 1)
ret[i][0] = 1;
else
break;
}//for
//矩阵首行
for (int j = 0; j < n; j++)
{
//无障碍,则有一条路径,否则不通
if (obstacleGrid[0][j] != 1)
ret[0][j] = 1;
else
break;
}//for
//其余位置
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
//当前位置为障碍,则到此处路径数为0
if (obstacleGrid[i][j] == 1)
ret[i][j] = 0;
else{
ret[i][j] = ret[i][j - 1] + ret[i - 1][j];
}//else
}//for
}//for
return ret[m - 1][n - 1];
}//uniques
};
LeetCode(63)Unique Paths II的更多相关文章
- 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). ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(63):不同路径 II
Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(95) Unique Binary Search Trees II
题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(96) Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
随机推荐
- python_pdb断点调试常用命令
Python pdb调试 在需要断点调试的地方,加上:import pdb;pdb.set_trace() h:打印当前版本pdb可用的命令,如果需要查询某个命令,可以输入 h [command]l: ...
- c语言程序设计案例教程(第2版)笔记(一)—零散、输入输出、最小公倍数、选择排序、冒泡排序
零散知识点: 非格式化输入输出:getchar().putchar() 格式化输入输出 :scanf().printf() 字符串输入输出 :gets() 或 scanf().puts() 或 ...
- [转]广义正交匹配追踪(gOMP)
广义正交匹配追踪(Generalized OMP, gOMP)算法可以看作为OMP算法的一种推广,由文献[1]提出,第1作者本硕为哈工大毕业,发表此论文时在Korea University攻读博士学位 ...
- Hdu 3605 Escape (最大流 + 缩点)
题目链接: Hdu 3605 Escape 题目描述: 有n个人要迁移到m个星球,每个星球有最大容量,每个人有喜欢的星球,问是否所有的人都能迁移成功? 解题思路: 正常情况下建图,不会爆内存,但是T ...
- 用WEKA进行数据挖掘
学习于IBM教学文档 数据挖掘学习与weka使用 第二部 分分类和集群 分类 vs. 群集 vs. 最近邻 在我深入探讨每种方法的细节并通过 WEKA 使用它们之前,我想我们应该先理解每个模型 - 每 ...
- 题解报告:hdu 1171 Big Event in HDU(多重背包)
Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...
- Service官方教程(7)Bound Service示例之1-同进程下直接继承Service
Extending the Binder class If your service is used only by the local application and does not need t ...
- Android最简单的实例 :环境搭建及HelloWorld
Android开发之旅:环境搭建及HelloWorld 2010-04-12 00:45 by 吴秦, 883961 阅读, 140 评论, 收藏, 编辑 ——工欲善其事必先利其器 引言 本系列适合 ...
- 1268 和为K的组合 Meet in mid二分思路
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1268&judgeId=193772 给出n = 20个数,问其是 ...
- SpringBoot_整合视图层技术
SpringBoot整合视图层技术 在目前的企业级应用开发中,前后端分离是趋势,但是视图层技术还占有一席之地.Spring Boot对视图层技术提供了很好的支持,官方推荐使用的模板引擎是Thymele ...