leetcode_63题——Unique Paths II(动态规划)

Unique Paths II

Total Accepted: 35061 Total Submissions: 125276My 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 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.

Hide Tags

Array Dynamic Programming

这道题只是在前面62题的基础上加了路障,也很简单,就是在路障这个地方就把它的路径的个数直接设为0

就好了,其它的和62题没啥区别

#include<iostream>
#include <vector>
using namespace std;

int uniquePathsWithObstacles(vector<vector<int> >& obstacleGrid) {
	if(obstacleGrid.empty())
		return 0;
	int m=obstacleGrid.size();
	int n=obstacleGrid[0].size();
	int **ary1;
	ary1=new int *[m];
	for(int i=0;i<m;i++)
		ary1[i]=new int[n];
	for(int i=0;i<m;i++)
		for(int j=0;j<n;j++)
			ary1[i][j]=obstacleGrid[i][j]-1;
	for(int i=m-1;i>=0;i--)
		for(int j=n-1;j>=0;j--)
			if(ary1[i][j]!=0)
			{
				if(i==m-1&&j==n-1)
					ary1[i][j]=1;
				else if(i==m-1&&j<n-1)
					ary1[i][j]=ary1[i][j+1];
				else if(i<m-1&&j==n-1)
					ary1[i][j]=ary1[i+1][j];
				else
					ary1[i][j]=ary1[i+1][j]+ary1[i][j+1];
			}
			int last_result=ary1[0][0];

			for(int i=0;i<m;i++)
				delete[]ary1[i];
			delete[]ary1;

			return last_result;
}
int main()
{

}

  

时间: 2024-08-01 20:32:37

leetcode_63题——Unique Paths II(动态规划)的相关文章

leetcode_62题——Unique Paths (动态规划)

Unique Paths Total Accepted: 45580 Total Submissions: 138958My Submissions Question Solution 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

[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之“动态规划”:Minimum Path Sum &amp;&amp; Unique Paths &amp;&amp; Unique Paths II

之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or righ

【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 II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Paths II Total Accepted: 13655 Total Submissions: 49081My Submissions Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would t

LeetCode开心刷题二十九天——63. Unique Paths II**

63. Unique Paths II Medium 938145FavoriteShare 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

[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 63 _ Unique Paths II 全部不同路径2

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 (marked

&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