zoukankan      html  css  js  c++  java
  • LeetCode 63 不同路径||

    二维BFS

    待更新

    二维DFS

    超出时间限制

    • 回溯条件
      • 遇到子节点为1,该路径走不通,回溯
      • 向右、向下超出边界
      • 到达终点
    • 怎路径数量
      • 路径不通: 返回0
      • 可以到达终点: 返回1
      • 当前节点向上返回所有子节点返回值之和
    class Solution_LC_63 {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            /*边界条件*/
            if(obstacleGrid==null || obstacleGrid.length==0 || obstacleGrid[0].length==0) return 0;
            if(obstacleGrid[0][0]==1) return 0; //起点为1,直接返回
    
            int totalPaths = dfs_rec(obstacleGrid, new int[]{0, 0});
    
            return totalPaths;
        }
        /**递归实现二维DFS
         * return: 子节点存在有效路径数量
         * */
        public int dfs_rec(int[][] obstacleGrid, int[] start){
            /*递归终止,叶子节点*/
            int x = start[0], y = start[1];
            if(x==obstacleGrid.length || y==obstacleGrid[0].length) return 0;   //越界
            else if(obstacleGrid[x][y]==1) return 0; //此路不通
            else if(x==(obstacleGrid.length-1) && y==(obstacleGrid[0].length-1)) return 1;   //正确路径
    
            /*递归过程*/
            int retVal = 0;
            retVal += dfs_rec(obstacleGrid, new int[]{x+1, y}); //向下
            retVal += dfs_rec(obstacleGrid, new int[]{x, y+1}); //向右
    
            /*返回值*/
            return retVal;
        }
    
    }
    

    动态规划

    执行用时:
    1 ms, 在所有 Java 提交中击败了49.66%的用户
    内存消耗:39.3 MB, 在所有 Java 提交中击败了48.15%的用户

    • 状态划分
      DP[i,j]表示从起点(0, 0)到达点(i, j)的路径总数

    • 状态转移
      1 从上一个点到达(i, j)只有两个方向: 右、下
      DP[i,j]DP[i-1,j]DP[i,j-1]有关
      2 点(i, j)值为0时才可能存在过该点的有效路径
      DP[i,j] = (matrix[i][j]==0)? DP[i-1,j]+DP[i,j-1]: 0

    • 初始状态
      1 起点
      DP[i,j] = (matrix[i][j]==0)? 1: 0
      2 上边界点,i=0
      DP[i,j] = (matrix[i][j]==0)? DP[i,j-1]: 0
      3 左边界点,j=0
      DP[i,j] = (matrix[i][j]==0)? DP[i-1,j]: 0

    class Solution_LC_63 {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            /*边界条件*/
            if(obstacleGrid==null || obstacleGrid.length==0 || obstacleGrid[0].length==0) return 0;
            if(obstacleGrid[0][0]==1) return 0; //起点为1,直接返回
    
            int rows = obstacleGrid.length, cols = obstacleGrid[0].length;
            int[][] DP = new int[rows][cols];
            /*递推过程*/
            for(int row=0; row<rows; row++){
                for(int col=0; col<cols; col++){
                    //起点
                    if(row==0 && col==0) DP[row][col] = (obstacleGrid[row][col]==0)? 1:0;
                    //左边界
                    else if(col==0)
                        DP[row][col] = (obstacleGrid[row][col]==0)? DP[row-1][col]:0;
                    //上边界
                    else if(row==0)
                        DP[row][col] = (obstacleGrid[row][col]==0)? DP[row][col-1]:0;
                    //其它点
                    else
                        DP[row][col] = (obstacleGrid[row][col]==0)? DP[row-1][col] + DP[row][col-1]:0;
                }
            }
    
            return DP[rows-1][cols-1];
        }
    
    }
    
  • 相关阅读:
    Ubuntu 16 安装redis客户端
    crontab 参数详解
    PHP模拟登录发送闪存
    Nginx配置端口访问的网站
    Linux 增加对外开放的端口
    Linux 实用指令之查看端口开启情况
    无敌的极路由
    不同的域名可以指向同一个项目
    MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error
    Redis 创建多个端口
  • 原文地址:https://www.cnblogs.com/CodeSPA/p/13255386.html
Copyright © 2011-2022 走看看