417. Pacific Atlantic Water Flow
正常做的,用了645MS。。感觉DFS的时候剪枝有问题。。
为了剪枝可能需要标记一个点的4种情况:
1:滨临大西洋,所有太平洋来的点可以通过;
2:濒临太平洋,所有大西洋来的点可以通过;
3:都不濒临,直接RETURN;
4:都濒临,通过;
这样的话用DP记录情况,应该能做。。
看了答案发现一开始思路就错了。。但是这个645MS居然能AC。。。。。。。。。。 正确思路看第二段代码吧。
public class Solution
{
public List<int[]> pacificAtlantic(int[][] matrix)
{
List<int[]> res = new ArrayList<int[]>();
if(matrix.length == 0) return res;
boolean[][] dp = new boolean[matrix.length][matrix[0].length];
for(int i = 0; i < matrix.length;i++)
for(int j = 0; j < matrix[0].length;j++)
{
boolean[] a = new boolean[2];
boolean[][] visited = new boolean[matrix.length][matrix[0].length];
helper(matrix,dp,visited,i,j,a);
if(a[0] && a[1])
{
int[] temp = new int[2];
temp[0] = i;
temp[1] = j;
dp[i][j] = true;
res.add(temp);
}
}
return res;
}
public void helper(int[][] map,boolean[][] dp,boolean[][] visited, int m,int n,boolean[] a)
{
int val = map[m][n];
if(dp[m][n])
{
a[0]=true;
a[1]=true;
return;
}
if(m == 0) a[0] = true;
else if(map[m-1][n] <= val && !visited[m-1][n])
{
visited[m-1][n] = true;
helper(map,dp,visited,m-1,n,a);
}
if(a[0]&&a[1]) return;
if(n == 0) a[0] = true;
else if(map[m][n-1] <= val && !visited[m][n-1])
{
visited[m][n-1] = true;
helper(map,dp,visited,m,n-1,a);
}
if(a[0]&&a[1]) return;
if(m == map.length-1) a[1] = true;
else if(map[m+1][n] <= val && !visited[m+1][n])
{
visited[m+1][n] = true;
helper(map,dp,visited,m+1,n,a);
}
if(a[0]&&a[1]) return;
if(n == map[0].length-1) a[1] = true;
else if(map[m][n+1] <= val && !visited[m][n+1])
{
visited[m][n+1] = true;
helper(map,dp,visited,m,n+1,a);
}
return;
}
}
看了讨论,发现自己好蠢,这个水平别去面试了,回家吃屎行了。

不一定需要遍历整个图。以滨临大西洋的做DFS,再以滨临太平洋的做DFS,合集就是要求的点。
public class Solution
{
public List<int[]> pacificAtlantic(int[][] matrix)
{
List<int[]> res = new ArrayList<int[]>();
if(matrix.length == 0) return res;
int m = matrix.length;
int n = matrix[0].length;
boolean[][] p = new boolean[m][n];
boolean[][] a = new boolean[m][n];
boolean[][] visited = new boolean[m][n];
for(int i = 0; i < matrix.length;i++)
{
p[i][0] = true;
a[i][n-1] = true;
visited = new boolean[m][n];
helper(matrix,p,i,0,visited);
visited = new boolean[m][n];
helper(matrix,a,i,n-1,visited);
}
for(int i = 0; i < matrix[0].length;i++)
{
p[0][i] = true;
a[m-1][i] = true;
visited = new boolean[m][n];
helper(matrix,p,0,i,visited);
visited = new boolean[m][n];
helper(matrix,a,m-1,i,visited);
}
for(int i = 0; i < m;i++)
for(int j = 0; j < n;j++)
if(a[i][j] == true && p[i][j] == true)
{
int[] temp = new int[2];
temp[0] = i;
temp[1] = j;
res.add(temp);
}
return res;
}
public void helper(int[][] matrix,boolean[][] map,int m, int n,boolean[][] visited)
{
if(visited[m][n]) return;
visited[m][n] = true;
map[m][n] = true;
int v = matrix[m][n];
if(m > 0 && matrix[m-1][n]>=v) helper(matrix,map,m-1,n,visited);
if(n > 0 && matrix[m][n-1]>=v) helper(matrix,map,m,n-1,visited);
if(m < map.length-1 && matrix[m+1][n] >= v) helper(matrix,map,m+1,n,visited);
if(n < map[0].length-1 && matrix[m][n+1] >= v) helper(matrix,map,m,n+1,visited);
}
}
417. Pacific Atlantic Water Flow的更多相关文章
- [LeetCode] 417. Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- LeetCode 417. Pacific Atlantic Water Flow
原题链接在这里:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ 题目: Given an m x n ma ...
- 【LeetCode】417. Pacific Atlantic Water Flow 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/pacific- ...
- 417 Pacific Atlantic Water Flow 太平洋大西洋水流
详见:https://leetcode.com/problems/pacific-atlantic-water-flow/description/ C++: class Solution { publ ...
- [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- Leetcode: Pacific Atlantic Water Flow
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- [Swift]LeetCode417. 太平洋大西洋水流问题 | Pacific Atlantic Water Flow
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- [LeetCode] Pacific Atlantic Water Flow 题解
题意 题目 思路 一开始想用双向广搜来做,找他们相碰的点,但是发现对其的理解还是不够完全,导致没写成功.不过,后来想清楚了,之前的错误可能在于从边界点进行BFS,其访问顺序应该是找到下一个比当前那个要 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- 配置wamp开发环境【2】 配置wamp开发环境之mysql的配置
此前我已经将wamp配置的Apache.PHP.phpmyadmin全部配置完成,以上三种配置参照 配置wamp开发环境 下面我们来看看mysql的配置,这里用的是mysql5.5.20,下载地址: ...
- ES 必备插件的安装
1. elasticsearch-head插件的安装,非常好的插件 elasticsearch-head是一个elasticsearch的集群管理工具,它是完全由html5编写的独立网页程序,你可以通 ...
- ES的安装运行
一.安装,运行 1. 检查java的版本环境 Elasticsearch依赖Java,在书写本文档的时候,推荐使用Oracle JDK 1.8.0_20 或 1.7.0_55以后的版本. 在安装Ela ...
- 常用sql笔记
Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname,T#) 课程表SC(S#,C#,score) 成绩表Teacher(T#,Tname) 教师表问题:1.查 ...
- Struts面试笔记
Struts2面试题1.struts2工作流程Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件. 核心控制器 ...
- pip assert_source_matches_version(self)版本验证报错Source in %s has version %s, which satisfies requirement %s的解决方式
在win8.1下为了安装flask模块,开始安装pip,结果发生了上篇博客里面的错误ntpath join(path, *paths) 发生UnicodeDecodeError.解决之后继续发现版本验 ...
- net Core 通过 Ef Core 访问、管理Mysql
net Core 通过 Ef Core 访问.管理Mysql 本文地址:http://www.cnblogs.com/likeli/p/5910524.html 环境 dotnet Core版本:1. ...
- 关于android app两次点击返回键退出的处理
现在的android app在开发时,引入了两次点击返回键退出app的设计 为了避免用户误触,这个设计很人性化 中文网上社区有些同学贴了一些实现的例子,我觉得不是很好 代码如下 public bool ...
- Codeforces 712C Memory and De-Evolution
Description Memory is now interested in the de-evolution of objects, specifically triangles. He star ...
- How Many Shortest Path
zoj2760:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2760 题意:给你一张有向带权图,然后问你最短路径有多少条. ...