61. Unique paths & unique paths II

Source: Internet
Author: User
Unique paths

A robot is located at the top-left corner ofMXNGrid (marked 'start' in the dimo-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 the dimo-below ).

How many possible unique paths are there?

Above is a 3x7 grid. How many possible unique paths are there?

Note: MAndNWill be at most 100.

Idea: in fact, the answer is C (m + N-2, s-1). But write programs using dynamic planning will be simple and fast. (For two codes, the first is easy to understand, and the second is based on the first optimization)

1.

Class solution {// C (m + N-2 m-1) Public: int uniquepaths (int m, int N) {vector <int> times (m, vector <int> (n, 0); For (INT r = 0; r <m; ++ R) Times [r] [0] = 1; for (INT c = 1; C <n; ++ c) Times [0] [c] = 1; // only once for (INT r = 1; r <m; ++ R) for (INT c = 1; C <n; ++ C) times [r] [c] = Times [r-1] [c] + Times [r] [C-1]; return times M-1] [n-1] ;}};

2.

Class solution {// C (m + N-2 m-1) Public: int uniquepaths (int m, int N) {If (M <= 0 | n <= 0) return 0; vector <int> r (n, 1); // record of a row for (INT r = 1; r <m; ++ R) for (INT c = 1; C <n; ++ c) R [c] = R [c] + R [C-1]; return R [n-1] ;}};

 

Unique paths II

 

Follow up for "unique paths ":

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

An obstacle and empty space is marked1And0Respectively in the grid.

For example,

There is one obstacle in the middle of a 3x3 grid as partitioned strated below.

[  [0,0,0],  [0,1,0],  [0,0,0]]

The total number of unique paths is2.

Note: MAndNWill be at most 100.

Idea: Same as above, only Initialization is complete 0. When the current position is 1, the number of steps before arrival is 0.

class Solution {public:    int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {        if(!obstacleGrid.size() || !obstacleGrid[0].size()) return 0;        int m = obstacleGrid.size(), n = obstacleGrid[0].size();        vector<int> R(n, 0);        R[0] = 1-obstacleGrid[0][0];        for(int r = 0; r < m; ++r)             for(int c = 0; c < n; ++c) {                if(c > 0)                    R[c] = (obstacleGrid[r][c] == 1 ? 0 : (R[c] + R[c-1]));                else if(obstacleGrid[r][c] == 1) R[0] = 0;            }        return R[n-1];    }};

 

61. Unique paths & 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.