**一. 题目描述**
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 3 3 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.
**二. 题目分析**
与上一题Unique Paths类似,但要特别注意第一列的障碍。在上一题中,第一列全部是1,但是在这一题中不同的是,第一列如果某一行有障碍物,那么后面的行应该为0。
**三. 示例代码**
使用动态规划:
~~~
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid)
{
const size_t x = obstacleGrid.size(); // 行数
const size_t y = obstacleGrid[0].size(); // 列数
vector<vector<int> > k;
for (int i = 0; i < x; ++i)
k.push_back(vector<int>(y, 0));
for (int i = 0; i < x; ++i)
{
if (obstacleGrid[i][0] == 0)
k[i][0] = 1;
else
{
for (int p = i; p < x; ++p)
k[p][0] = 0;
break;
}
}
for (int j = 0; j < y; ++j)
{
if (obstacleGrid[0][j] == 0)
k[0][j] = 1;
else
{
for (int q = j; q < y; ++q)
k[0][q] = 0;
break;
}
}
for (int i = 1; i < x; ++i)
{
for (int j = 1; j < y; ++j)
{
if (obstacleGrid[i][j] != 0)
k[i][j] = 0;
else
k[i][j] = k[i - 1][j] + k[i][j - 1];
}
}
return k[x - 1][y - 1];
}
};
~~~
![](https://box.kancloud.cn/2016-01-05_568bb5f047d5a.jpg)
**四. 小结**
[](http://blog.csdn.net/liyuefeilong/article/details/49520819#)[](http://blog.csdn.net/liyuefeilong/article/details/49520819# "分享到QQ空间")[](http://blog.csdn.net/liyuefeilong/article/details/49520819# "分享到新浪微博")[](http://blog.csdn.net/liyuefeilong/article/details/49520819# "分享到腾讯微博")[](http://blog.csdn.net/liyuefeilong/article/details/49520819# "分享到人人网")[](http://blog.csdn.net/liyuefeilong/article/details/49520819# "分享到微信")
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching