[LintCode]unique paths with obstacles

http://www.lintcode.com/zh-cn/problem/unique-paths-ii/

在上一题的基础上加入了障碍物。

同样可采用递归实现,递推公式不变,但是需要加入对障碍物的判断。

下面是实现的代码:

 1 #include <cstring>
 2 #include <cmath>
 3 class Solution {
 4 public:
 5     /**
 6      * @param obstacleGrid: A list of lists of integers
 7      * @return: An integer
 8      */
 9     int solute(vector<vector<int> > &v, vector<vector<int> > &f, int m, int n)
10     {
11         if(f[m][n]!=-1)   return f[m][n];
12         int M = v.size(), N = v[0].size();
13         if(v[M-m][N-n]==1)  f[m][n] = 0;
14         else if(m==1&&n==1) f[m][n] = 1;
15         else if(m==1)    f[m][n] = min(1, solute(v, f, 1, n-1));
16         else if(n==1)    f[m][n] = min(1, solute(v, f, m-1, 1));
17         else    f[m][n] = solute(v, f, m-1, n)+solute(v, f, m, n-1);
18         return f[m][n];
19     }
20     int uniquePathsWithObstacles(vector<vector<int> > &v) {
21         // write your code here
22         int m = v.size(), n = v[0].size();
23         vector<vector<int> > f(m+1, vector<int>(n+1, -1));
24         return solute(v, f, m, n);
25     }
26 };
时间: 2024-11-09 04:52:16

[LintCode]unique paths with obstacles的相关文章

LintCode : Unique Paths II

Problem Description: 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. Code: public class Solutio

LintCode : Unique Paths

Problem Description: 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

62. 63. Unique Paths 64. Minimum Path Sum

1. 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' i

&lt;LeetCode OJ&gt; 63. Unique Paths II

63. Unique Paths II My Submissions Question Total Accepted: 55136 Total Submissions: 191949 Difficulty: Medium Follow up for "Unique Paths":紧接着上一题"唯一路劲",现在考虑有一些障碍在网格中,无法到达,请重新计算到达目的地的路线数目 Now consider if some obstacles are added to the

[LeetCode]20. 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 middl

Java for LeetCode 063 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 middl

LeetCode --- 63. Unique Paths II

题目链接: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

Unique Paths II (dp题)

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 middl

【LeetCode】063. 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 m