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];
        }
    
    }
    
  • 相关阅读:
    BZOJ3697: 采药人的路径
    解题:WC 2007 石头剪刀布
    解题:CQOI 2017 老C的方块
    解题:洛谷4314 CPU监控
    解题:CQOI 2017 老C的任务
    解题:CF1009 Dominant Indices
    解题:CF570D Tree Requests
    解题:APIO 2012 派遣
    解题:ZJOI 2015 幻想乡战略游戏
    解题:洛谷4178 Tree
  • 原文地址:https://www.cnblogs.com/CodeSPA/p/13255386.html
Copyright © 2011-2022 走看看