leetcode-unique paths 2

The feeling of depending on oneself and AC is just great.

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 class Solution {
 5 public:
 6     int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
 7         vector<vector<int>> d(obstacleGrid.size(), vector<int>(obstacleGrid[0].size(), 0));
 8         for (int i = 0; i < d.size(); i++)
 9         {
10             if (obstacleGrid[i][0] != 1)
11                 d[i][0] = 1;
12             else break;
13         }
14         for (int i = 0; i < d[0].size(); i++)
15         {
16             if (obstacleGrid[0][i] != 1)
17                 d[0][i] = 1;
18             else break;
19         }
20         for (int i = 1; i < obstacleGrid.size(); i++)
21         {
22             for (int j = 1; j < obstacleGrid[i].size(); j++)
23             {
24                 if (obstacleGrid[i][j] != 1)///////////////////////////////////////
25                 {
26                     d[i][j] = d[i - 1][j] + d[i][j - 1];
27                 }
28                 //cout << d[i][j] << " ";
29             }
30             //cout << endl;
31         }
32         return d[obstacleGrid.size()-1][obstacleGrid[0].size()-1];
33     }
34 };
35 int main()
36 {
37     Solution s;
38     vector<vector<int>> obstacleGrid;
39     int a0[] = { 0, 0, 0 };
40     int a1[] = { 0, 1, 0 };
41     int a2[] = { 0, 0, 0 };
42     obstacleGrid.push_back(vector<int>(a0, a0 + 3));
43     obstacleGrid.push_back(vector<int>(a1, a1 + 3));
44     obstacleGrid.push_back(vector<int>(a2, a2 + 3));
45
46     //print vector<vector<int>>
47     //for (int i = 0; i < obstacleGrid.size(); i++)
48     //{
49     //    for (int j = 0; j < obstacleGrid[i].size(); j++)
50     //    {
51     //        cout << obstacleGrid[i][j]<<" ";
52     //    }
53     //    cout << endl;
54     //}
55     cout << s.uniquePathsWithObstacles(obstacleGrid) << endl;
56     return 0;
57 }
时间: 2024-10-29 19:07:06

leetcode-unique paths 2的相关文章

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution 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 spac

[leetcode]Unique Paths II @ Python

原题地址:https://oj.leetcode.com/problems/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 i

[leetcode]Unique Paths @ Python

原题地址:https://oj.leetcode.com/problems/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 t

[LeetCode] 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] Unique Paths &amp;&amp; Unique Paths II &amp;&amp; Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths https://oj.leetcode.com/problems/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 rea

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 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 t

LeetCode——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]Unique Paths

题目: 从左上角到右下角的所有可能路径. 思路1: 回溯法去递归遍历所有的路径,但是复杂度太大,无法通过.checkPath方法实现 动态规划法,从左上角到每一格的路径数与它的上面一格和左边一格的路径和: N(m,n)=N(m-1,n)+N(m,n-1): 注意:第一行和第一列的特殊情况. package com.example.medium; /** * A robot is located at the top-left corner of a m x n grid (marked 'Sta

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 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 t

leetCode —— Unique Paths II [Dynamic Programming]

唯一路径问题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