LeetCode | Unique Paths I,II

100 篇文章 0 订阅
9 篇文章 0 订阅

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.

咋一看,深搜之,直接超时~~
然后发现是可以拿DP做的,思考出来一个DP方程,虽然不是很优化但是一次过了还是很开心的
以下代码展现了一些思考过程:
1、深搜
2、visit标记数组,仔细分析发现不会走过已经走的路
3、dp[i][j]=dp[i-1][j]+dp[i][j-1] 一开始想着倒推,然后一想似乎不能倒推,是从第一步开始走的

class Solution {
public:
    int dir[2][2]={{1,0},{0,1}};
    int route=0;
    int m,n;
    int uniquePaths(int m, int n) {
        this->m=m;
        this->n=n;
        // vector<vector<char> > visit(m,n);
        // char visit[m][n];
        // memset(visit,0,sizeof(visit));
        // dfs(1,1);
        // return route;
        // vector<vector<int> > dp(m,n);
        int dp[m+2][n+2];
        memset(dp,0,sizeof(dp));
        dp[1][1]=1;

        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                dp[i][j+1]=dp[i][j]+dp[i-1][j+1];
                dp[i+1][j]=dp[i][j]+dp[i+1][j-1];
            }
        }

        return dp[m][n];
        //不能倒推
        // for(int i=m-1;i>0;i--){
        //     for(int j=n-1;j>0;j--){
        //         dp[i][j]=dp[i-1][j]+dp[i][j-1];
        //     }
        // }
    }

    void dfs(int x,int y){
        if(x==m && y==n){
            route++;
            return;
        }

        for(int i=0;i<2;i++){
            int next_x=x+dir[i][0];
            int next_y=y+dir[i][1];
            //只存在越界,不存在反复访问
            if(next_x>m || next_y>n) continue;
            // if(visit[next_x][next_y])
            dfs(next_x,next_y);
        }
    }
};

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.

相比较I的话,II只是增加了一点障碍,使得在进入DP的时候需要一些条件。
由于不小心写错了一个条件所以导致debug好久

不过执行的时间似乎有点长,还有待优化:
参考链接:https://discuss.leetcode.com/topic/15267/4ms-o-n-dp-solution-in-c-with-explanations

class Solution {
public:
    int m,n;
    vector<vector<int> > obstacleGrid;

    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int m=obstacleGrid.size();
        if(m==0) return 0;
        int n=obstacleGrid[0].size();
        // if(m==1 && n==1) return !obstacleGrid[0][0];
        if(obstacleGrid[0][0]) return 0;

        int dp[m+2][n+2];
        this->m=m;
        this->n=n;
        this->obstacleGrid=obstacleGrid;
        memset(dp,0,sizeof(dp));
        dp[1][1]=1;

        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                if(!reachAble(i,j)) continue;
                //这一点是可达的
                if(reachAble(i,j+1)){
                    dp[i][j+1]=dp[i][j]+reachAble(i-1,j+1)*dp[i-1][j+1];
                }
                if(reachAble(i+1,j)){
                    dp[i+1][j]=dp[i][j]+reachAble(i+1,j-1)*dp[i+1][j-1];
                }
            }
        }
        return dp[m][n];
    }

    int reachAble(int x,int y){
        int i=x-1,j=y-1;
        if(i>=m || i<0 || j>=n || j<0) return 0;
        return !obstacleGrid[i][j];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值