码迷,mamicode.com
首页 > 其他好文 > 详细

62. 63. Unique Paths 64. Minimum Path Sum

时间:2016-03-12 22:51:03      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

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‘ in the diagram below).

How many possible unique paths are there?

技术分享

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

Note: m and n will be at most 100.

class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int>> v(m, vector<int>(n, 0));
        int i, j;
        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                if(0 == i || 0 == j)
                    v[i][j] = 1;
                else
                    v[i][j] = v[i-1][j] + v[i][j-1];
            }
        }
        return v[m-1][n-1];
    }
};

 

2.

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.

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

 

3.

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 right at any point in time.

int minPathSum(vector<vector<int> > &grid) {
    if (grid.size()<=0){
        return 0;
    }
    int i, j;
    for(i=0; i<grid.size(); i++){
        for(j=0; j<grid[i].size(); j++){
            int top = i-1<0 ? INT_MAX : grid[i-1][j] ;
            int left = j-1<0 ? INT_MAX : grid[i][j-1];
            if (top==INT_MAX && left==INT_MAX){
                continue;
            }
            grid[i][j] += (top < left? top: left);
        }
    }
    return grid[grid.size()-1][grid[0].size()-1];
}

 

62. 63. Unique Paths 64. Minimum Path Sum

标签:

原文地址:http://www.cnblogs.com/argenbarbie/p/5270183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!