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). ...
随机推荐
- 《Linux Shell 脚本攻略》读书笔记
本书主要讲解一些linux shell命令的用法,讲解一些shell的奇技淫巧. 第一章 小试牛刀 主要介绍一些基本shell指令 终端打印:echo.printf 别名:alias 终端处理工具:t ...
- TCP(Transmission Control Protocol)学习笔记
一.TCP(Transmission Control Protocol)原理介绍(参考维基百科) TCP连接包括三种状态:连接建立.数据传送和连接终止. TCP用三路握手(three-way hand ...
- 【BZOJ5338】[TJOI2018]异或(主席树)
[BZOJ5338][TJOI2018]异或(主席树) 题面 洛谷 题解 很明显的是\(Trie\)树上暴力判断答案 因为要支持区间,用主席树的结构存\(Trie\)树就好了 #include< ...
- BZOJ4104:[Thu Summer Camp 2015]解密运算——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4104 对于一个长度为N的字符串,我们在字符串的末尾添加一个特殊的字符".".之 ...
- SCWS中文分词,向xdb词库添加新词
SCWS是个不错的中文分词解决方案,词库也是hightman个人制作,总不免有些不尽如人意的地方.有些词语可能不会及时被收入词库中. 幸好SCWS提供了词库XDB导出导入词库的工具(phptool_f ...
- Redis Scan迭代器遍历操作原理(二)
续上一篇文章 Redis Scan迭代器遍历操作原理(一)–基础 ,这里着重讲一下dictScan函数的原理,其实也就是redis SCAN操作最有价值(也是最难懂的部分). 关于这个算法的源头,来自 ...
- MapReduce(一) mapreduce基础入门
一.mapreduce入门 1.什么是mapreduce 首先让我们来重温一下 hadoop 的四大组件:HDFS:分布式存储系统MapReduce:分布式计算系统YARN: hadoop 的资源调度 ...
- [知识点]C++中STL容器之set
零.STL目录 1.容器之map 2.容器之vector 3.容器之set 一.前言 继上期的vector之后,我们又迎来了另一个类数组的STL容器——set. 二.用途与特性 set,顾名思义,集合 ...
- [雅礼集训 2017 Day1]市场
link 试题分析 可以容易发现此题维护的是一个数据结构,支持区间加,区间除,区间查询最大值.其实就是在$\log$级复杂度内维护除法操作. 我们发现当除数很大或者此串序列大小差不多时,我们令$a_i ...
- Git 自动补全
如果你用的是 Bash shell,可以试试看 Git 提供的自动补全脚本. http://git-scm.com/download 下载 Git 的源代码,进入contrib/completion ...