不同的路径 II

class Solution {
public:
    /**
     * @param obstacleGrid: A list of lists of integers
     * @return: An integer
     */ 
    int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
        // write your code here
        int m = obstacleGrid.size();
        if(m==0) return 0;
        int n = obstacleGrid[0].size();
        int a[m][n];
        int temp=100;
        for(int i=0;i<m;++i){
            a[i][0]=1;
            if(obstacleGrid[i][0]==1){
                temp = i;
                a[i][0]=0;
            }
            if(i>=temp)
                a[i][0]=0;
        }
        temp = 100;
        for(int j=0;j<n;++j){
            a[0][j]=1;
            if(obstacleGrid[0][j]==1){
                temp = j;
                a[0][j]=0;
            }
            if(j>=temp)
                a[0][j]=0;
        }
        for(int i =1;i < m;++i){
            for(int j =1;j<n;++j){
                a[i][j]=a[i-1][j]+a[i][j-1];
                if(obstacleGrid[i][j]==1)
                    a[i][j]=0;
            }
        }
        return a[m-1][n-1];
    }
};

 

posted @ 2015-09-09 19:13  影翕  阅读(206)  评论(0编辑  收藏  举报