63.UniquePaths II---dp
题目大意:与62题类似,只是这个题中间有障碍。
法一:dfs,依旧超时。代码如下:
     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
         boolean vis[][] = new boolean[obstacleGrid.length][obstacleGrid[0].length];
         int f[][] = {{1, 0}, {0, 1}};
         //如果起始格子就是障碍,则表示不能正常到达目的地
         if(obstacleGrid[0][0] == 1) {
             return 0;
         }
         return dfs(obstacleGrid, 0, 0, 0, f, vis);
     }
     public static int dfs(int[][] obstacleGrid, int x, int y, int cnt, int f[][], boolean vis[][]) {
         if(x == obstacleGrid.length - 1 && y == obstacleGrid[0].length - 1 && obstacleGrid[x][y] == 0) {
             cnt++;
             return cnt;
         }
         for(int i = 0; i < 2; i++) {
             int cnt_x = x + f[i][0];
             int cnt_y = y + f[i][1];
             if(cnt_x < obstacleGrid.length && cnt_y < obstacleGrid[0].length && vis[cnt_x][cnt_y] == false && obstacleGrid[cnt_x][cnt_y] == 0) {
                 vis[cnt_x][cnt_y] = true;
                 cnt = dfs(obstacleGrid, cnt_x, cnt_y, cnt, f, vis);
                 vis[cnt_x][cnt_y] = false;
             }
         }
         return cnt;
     }
法二:dp,模仿62题的dp,只是这里要考虑障碍。代码如下(耗时2ms):
     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
         //如果起始位置或结束位置是1,则直接不通
         if(obstacleGrid[0][0] == 1 || obstacleGrid[obstacleGrid.length - 1][obstacleGrid[0].length - 1] == 1) {
             return 0;
         }
         int dp[][] = new int[obstacleGrid.length][obstacleGrid[0].length];
         //初始化第一列
         for(int i = 0; i < obstacleGrid.length; i++) {
             if(obstacleGrid[i][0] == 0) {
                 dp[i][0] = 1;
             }
             else {//第一列,一旦碰到一个障碍1,则第一列障碍下面的所有都是障碍,不通
                 for(; i < obstacleGrid.length; i++) {
                     dp[i][0] = 0;
                 }
                 break;
             }
         }
         //初始化第一行
         for(int i = 0; i < obstacleGrid[0].length; i++) {
             if(obstacleGrid[0][i] == 0) {
                 dp[0][i] = 1;
             }
             else {//第一行,一旦碰到一个障碍1,则第一行障碍后面的所有都是障碍,不通
                 for(; i < obstacleGrid[0].length; i++) {
                     dp[0][i] = 0;
                 }
                 break;
             }
         }
         //计算dp
         for(int i = 1; i < obstacleGrid.length; i++) {
             for(int j = 1; j < obstacleGrid[0].length; j++) {
                 if(obstacleGrid[i][j] == 1) {//如果当前格是障碍,则不可通
                     dp[i][j] = 0;
                 }
                 else {
                     dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                 }
             }
         }
         return dp[obstacleGrid.length - 1][obstacleGrid[0].length - 1];
     }
法三:一维dp,与62题很类似,但是要比62题难思考一点,因为要考虑障碍的问题,而且没有初始化,直接从0开始遍历的。代码如下(耗时1ms):
     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
         if(obstacleGrid[0][0] == 1 || obstacleGrid[obstacleGrid.length - 1][obstacleGrid[0].length - 1] == 1) {
             return 0;
         }
         int[] dp = new int[obstacleGrid[0].length];
         dp[0] = 1;
         //从0开始遍历,否则会漏掉第一列,因为其实第一列并没有初始化
         for(int i = 0; i < obstacleGrid.length; i++) {
             for(int j = 0; j < obstacleGrid[0].length; j++) {
                 //遇到障碍则赋0
                 if(obstacleGrid[i][j] == 1) {
                     dp[j] = 0;
                 }
                 //由于j是从0开始,所以只考虑>0的情况
                 else if(j > 0){
                     dp[j] += dp[j - 1];
                 }
             }
         }
         return dp[obstacleGrid[0].length - 1];
     }
63.UniquePaths II---dp的更多相关文章
- LeetCode-Palindrome Partitioning II[dp]
		Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ... 
- hdu 1207 汉诺塔II (DP+递推)
		汉诺塔II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ... 
- #1560 : H国的身份证号码II(dp+矩阵快速幂)
		#1560 : H国的身份证号码II 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 H国的身份证号码是一个N位的正整数(首位不能是0).此外,由于防伪需要,一个N位正整 ... 
- poj-2336 Ferry Loading II(dp)
		题目链接: Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3946 Accepted: ... 
- HDOJ 5087 Revenge of LIS II DP
		DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ... 
- hihocoder 1828 Saving Tang Monk II (DP+BFS)
		题目链接 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great C ... 
- hdu5087 Revenge of LIS II (dp)
		只要理解了LIS,这道题稍微搞一下就行了. 求LIS(最长上升子序列)有两种方法: 1.O(n^2)的算法:设dp[i]为以a[i]结尾的最长上升子序列的长度.dp[i]最少也得是1,就初始化为1,则 ... 
- [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II   dp
		4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec Memory Limit: 128 MBSubmi ... 
- leetcode-63. Unique Paths II · DP + vector
		题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ... 
随机推荐
- 【bzoj1430】小猴打架  Prufer序列
			题目描述 给出 $n$ 个点,每次选择任意一条边,问这样 $n-1$ 次后得到一棵树的方案数是多少. 输入 一个整数N. 输出 一行,方案数mod 9999991. 样例输入 4 样例输出 96 题解 ... 
- [您有新的未分配科技点]博弈论入门:被博弈论支配的恐惧(Nim游戏,SG函数)
			今天初步学习了一下博弈论……感觉真的是好精妙啊……希望这篇博客可以帮助到和我一样刚学习博弈论的同学们. 博弈论,又被称为对策论,被用于考虑游戏中个体的预测行为和实际行为,并研究他们的应用策略.(其实这 ... 
- TCP的拥塞控制 (二)
			TCP Reno TCP Reno引入了ssthresh(Slow Start threshold)变量,作为TCP的Slow Start和Congestion Avoidance两个阶段的分界线. ... 
- 【BZOJ3166】ALO(主席树)
			[BZOJ3166]ALO(主席树) 题面 权限题qwq 资磁洛谷 题解 用一个\(set\)求出左右侧比这个数大的第\(2\)个数, 然后用可持久化\(Trie\)算一下就好啦 #include&l ... 
- 【BZOJ4568】幸运数字(线性基,树链剖分,ST表)
			[BZOJ4568]幸运数字(线性基,树链剖分,ST表) 题面 BZOJ Description A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市 ... 
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code   【区间dp】
			农民约翰收到一条的消息,记该消息为长度至少为2,只由大写字母组成的字符串S,他通过一系列操作对S进行加密. 他的操作为,删除S的前面或者后面的若干个字符(但不删光整个S),并将剩下的部分连接到原字符串 ... 
- MyBatis  openSession(),close(),和commit()  底层代码剖析
			一:MyBatis工具类 中openSession到底做了什么? Mybatis工具类 private static final String RESOURCE = "mybatis-con ... 
- 洛谷 P1486 BZOJ 1503 NOI 2004  郁闷的出纳员 fhq treap
			思路: 1. 此处的fhq treap的分裂是按照权值分裂然后插入的.将小于k的分为一棵子树,大于等于k的分为另一棵子树. 2. 删除的时候只要将大于等于min的分裂到以root为根的树中,另一部分不 ... 
- django2.0 uwsgi nginx
			[TOC]# 1.安装pip```sudo apt-get updatesudo apt-get install python-pip```# 2.使用pip 安装virtualenv 和 virtu ... 
- Android开发-eclipse+phonegap(Cordova)环境搭建
			搭建步骤: 一.安装java [官网下载].eclipse+ADT+Android SDK [点我下载x86(android-22)] | [adt-bundle-windows-x86_64-201 ... 
