"One Day together Leetcode" #63. Unique Paths II

Source: Internet
Author: User

One Day together Leetcode (i) Title

Follow up for "Unique Paths":

Now consider if some obstacles is added to the grids. How many unique paths would there be?

An obstacle and empty space are 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.

(ii) Problem solving

Problem-solving ideas: Refer to the previous blog post "One Day together Leetcode" #62. Unique Paths

classSolution { Public:intdp[101][101];intUniquepathswithobstacles ( vector<vector<int>>& Obstaclegrid) {introw = Obstaclegrid.size ();intCol =0;if(row!=0) col = obstaclegrid[0].size ();if(obstaclegrid[0][0]==1)return 0;//Starting point not to be returned directly 0         for(inti = row-1; i>=0; i--) for(intj = col-1; j>=0; j--) {if(obstaclegrid[i][j]==1) Dp[i][j] =0;//Representative blocked                Else if(i==row-1&&j==col-1) Dp[i][j] =1;//The DP at the specified end is 1                ElseDP[I][J] = dp[i+1][j]+dp[i][j+1]; }returndp[0][0]; }

"One Day together Leetcode" #63. Unique Paths II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.