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). ...
随机推荐
- robot framework Selenium2关键字介绍
*** Settings *** Library Selenium2Library *** Keywords *** Checkbox应该不被选择 [Arguments] ${locator} Che ...
- C++解析(11):对象的构造
0.目录 1.对象的初始化 2.构造函数 3.无参构造函数与拷贝构造函数 4.小结 1.对象的初始化 对象中成员变量的初始值是多少? 下面的类定义中成员变量i和j的初始值是什么? 从程序设计的角度,对 ...
- CF 566A Matching Names
CF 566A Matching Names 题目描述 给出n个名字和n个昵称,求一个名字和昵称的劈配方案,使得被劈配的名字和昵称的最长公共前缀长度的和最大. 1<=n<=100000 字 ...
- (转)Ubuntu 17.04_64上搭建巡风扫描系统(资产信息漏洞扫描内网神器)
巡风简介 巡风是一款适用于企业内网的漏洞快速应急.巡航扫描系统,通过搜索功能可清晰的了解内部网络资产分布情况,并且可指定漏洞插件对搜索结果进行快速漏洞检测并输出结果报表.其主体分为两部分:网络资产识别 ...
- 【bzoj3105】新Nim游戏
Portal--> bzoj3105 新Nim游戏 Solution 转化一下问题 首先看一下原来的Nim游戏,先手必胜的条件是:每堆数量的异或和不为\(0\) 所以在新的游戏中,如果要保证自己 ...
- 网络协议之mDNS20170217
DNS(Domain Name System,域名系统)因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最终得 ...
- vs2003一查找就卡死了
Visual Studio 2003一查找就卡死了.解决办法如下: win7 32位下 解决方法:找到VS2003的安装目录,修改"...\Microsoft Visual Studio . ...
- poj1659 Frogs' Neighborhood
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10239 Accepted: 4 ...
- 洛谷P1078 文化之旅
P1078 文化之旅 1.1K通过 3.6K提交 题目提供者洛谷OnlineJudge 标签NOIp普及组2012 难度普及+/提高 时空限制1s / 128MB 提交 讨论 题解 最新讨论更多讨 ...
- LINUX安全加固操作
1.禁止Ctrl-Alt-Delete组合键重启系统 vi /etc/inittab #ca::ctrlaltdel:/sbin/shutdown -t3 -r now 如果还存在下面的文件,则需要注 ...