当前位置: 首页 > news >正文

迷宫里的动态规划应用

[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 \begin{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];
    }
}

转载于:https://www.cnblogs.com/echie/p/9594499.html

相关文章:

  • Django学习手册 - cookie / session
  • We are unable to complete the review of your app since one or more of your In App Purchases have not
  • IOS内存管理
  • gerrit + ldap + phpldapadmin docker部署
  • 【编程之美】2.1 - 求二进制数中1的个数
  • JavaScript中数组的排序方法:1.冒泡排序 2.选择排序
  • js计算页面加载时间
  • Solium代码测试框架
  • 迎接第五次工业革命浪潮,不当纳米知识文盲
  • 12-单表查询
  • Microsoft Component Designer 设计组件一例
  • 百度云高速下载Pandownload
  • CF卡格式化XPE启动盘
  • BZOJ 3224: Tyvj 1728 普通平衡树 or 洛谷 P3369 【模板】普通平衡树-Splay树模板题
  • Linux 抓取网页实例(shell+awk)
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • Mysql优化
  • Netty 框架总结「ChannelHandler 及 EventLoop」
  • vuex 笔记整理
  • Vue官网教程学习过程中值得记录的一些事情
  • web标准化(下)
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 关于Flux,Vuex,Redux的思考
  • 基于Mobx的多页面小程序的全局共享状态管理实践
  • 时间复杂度与空间复杂度分析
  • 温故知新之javascript面向对象
  • 项目管理碎碎念系列之一:干系人管理
  • 如何在招聘中考核.NET架构师
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (ros//EnvironmentVariables)ros环境变量
  • (solr系列:一)使用tomcat部署solr服务
  • (多级缓存)多级缓存
  • (二十四)Flask之flask-session组件
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (转)树状数组
  • *Django中的Ajax 纯js的书写样式1
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET 6 在已知拓扑路径的情况下使用 Dijkstra,A*算法搜索最短路径
  • .php结尾的域名,【php】php正则截取url中域名后的内容
  • .pub是什么文件_Rust 模块和文件 - 「译」
  • :=
  • @media screen 针对不同移动设备
  • @Transactional 竟也能解决分布式事务?
  • [AIGC] MySQL存储引擎详解
  • [BZOJ2850]巧克力王国
  • [C++]模板与STL简介
  • [Docker]四.Docker部署nodejs项目,部署Mysql,部署Redis,部署Mongodb
  • [ExtJS5学习笔记]第三十节 sencha extjs 5表格gridpanel分组汇总
  • [ffmpeg] av_opt_set 解析
  • [Godot] 3D拾取
  • [Google Guava] 2.1-不可变集合
  • [HCIE] IPSec-VPN (手工模式)
  • [hdu 1247]Hat’s Words [Trie 图]