zoukankan      html  css  js  c++  java
  • 迷宫里的动态规划应用

    [LeetCode 63] Unique Paths II

    题目

    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).
    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.
    Note: m and n will be at most 100.
    

    测试案例

    Input:
    [
      [0,0,0],
      [0,1,0],
      [0,0,0]
    ]
    Output: 2
    Explanation:
    There is one obstacle in the middle of the 3x3 grid above.
    There are two ways to reach the bottom-right corner:
    1. Right -> Right -> Down -> Down
    2. Down -> Down -> Right -> Right
    

    思路

    记 nums[i][j] 表示从 (i,j) 点到达右下角的不同路径数。那么有如下递归式:

    [nums[i][j] = lbrace egin{align} nums[i + 1][j] + num[i][j + 1] , ;(i,j) 处无障碍 \ 0,;(i,j) 处有障碍 end{align} ]

    代码如下

    class Solution {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            int m, n;        
            if((m = obstacleGrid.length) < 1 || (n = obstacleGrid[0].length) < 1){
                return 0;
            }                
            int[][] nums = new int[m][n];
            if((nums[m - 1][n - 1] = 1 - obstacleGrid[m - 1][n - 1]) == 0){
                return 0;
            }
            for(int i = n - 2; i > -1; i--){
                if(obstacleGrid[m - 1][i] == 1){
                    nums[m - 1][i] = 0;
                }
                else{
                    nums[m - 1][i] = nums[m - 1][i + 1];
                }            
            }
            for(int i = m - 2; i > -1; i--){
                if(obstacleGrid[i][n - 1] == 1){
                    nums[i][n - 1] = 0;
                }
                else{
                    nums[i][n - 1] = nums[i + 1][n - 1];    
                }            
            }
            for(int i = m - 2; i > -1; i--){
                for(int j = n - 2; j > -1; j--){
                    if(obstacleGrid[i][j] == 1){
                        nums[i][j] = 0;
                    }
                    else{
                        nums[i][j] = nums[i + 1][j] + nums[i][j + 1];
                    }
                }
            }
            return nums[0][0];
        }
    }
    

    [LeetCode 64] Minimum Path Sum

    题目

    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.
    

    测试案例

    Input:
    [
      [1,3,1],
      [1,5,1],
      [4,2,1]
    ]
    Output: 7
    Explanation: Because the path 1→3→1→1→1 minimizes the sum.
    

    代码如下

    class Solution {
        public int minPathSum(int[][] grid) {
            int m = grid.length, n = grid[0].length;        
            int[][] price = new int[m][n];
            price[m - 1][n - 1] = grid[m - 1][n - 1];
            for(int i = n -2; i > -1; i--){
                price[m - 1][i] = price[m - 1][i + 1] + grid[m - 1][i];
            }
            for(int i = m - 2; i > -1; i--){
                price[i][n - 1] = price[i + 1][n - 1] + grid[i][n - 1];
            }
            for(int i = m - 2; i> -1; i--){
                for(int j = n -2; j > -1; j--){
                    price[i][j] = price[i + 1][j];
                    if(price[i][j] > price[i][j + 1]){
                        price[i][j] = price[i][j + 1];
                    }
                    price[i][j] += grid[i][j];
                }
            }
            return price[0][0];
        }
    }
    
  • 相关阅读:
    Office2003中文绿色免安装版本(wordExcelPowerPoint三合一)
    图片去背景神奇网站remove.bg
    网络视频下载分享
    Windows10手机投屏到电脑
    测试开发工程必备技能之一:Mock的使用
    实战教程:如何将自己的Python包发布到PyPI上
    Sysbench测试神器:一条命令生成百万级测试数据
    Java 获取PDF中的数字签名信息
    Java 在Excel中添加分离型饼图、环形图
    C#/VB.NET 添加、删除PPT幻灯片中的数字签名
  • 原文地址:https://www.cnblogs.com/echie/p/9594499.html
Copyright © 2011-2022 走看看