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];
        }
    }
    
  • 相关阅读:
    【网络编程3】网络编程基础-arp请求(局域网主机扫描)
    【CTF MISC】隐写术wireshark找出图片-“强网杯”网络安全挑战赛writeup
    【网络编程2】网络编程基础-发送ICMP包(Ping程序)
    【网络编程1】网络编程基础-TCP、UDP编程
    【漏洞分析】两个例子-数组溢出修改返回函数与strcpy覆盖周边内存地址
    【Python】zip文件密码破解
    【bzoj1923】[Sdoi2010]外星千足虫 高斯消元
    spring cloud zuul在使用中遇到的坑 : 转发时自动去掉prefix
    Eclipse MAT:浅堆 vs 保留堆
    Java SDK夯住(Hang)问题排查
  • 原文地址:https://www.cnblogs.com/echie/p/9594499.html
Copyright © 2011-2022 走看看