当前位置:网站首页>417.太平洋大西洋水流问题
417.太平洋大西洋水流问题
2022-07-30 05:38:00 【Linke66】
解题思路:
一开始自己的思路是看每一个点是否能流到太平洋和大西洋,很复杂,各种细节考虑不到。看了题解觉得这个方法很好,记录一下。
我们从四个边开始往高处走,能走到的位置就是能流到对应边旁边的洋流。
can_reach_p,(太平洋 pacific)代表能到达太平洋;
can_reach_a,(大西洋 atlantic)代表能到达大西洋。
假如某个点的can_reach_p和can_reach_a都为true,说明符合条件。
具体思路看代码。
class Solution {
public:
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
if(heights.empty()||heights[0].empty()) return {
};
int m=heights.size(),n=heights[0].size();
vector<vector<int>>ret;
vector<vector<bool>> can_reach_p(m,vector<bool>(n,false)); //能到达太平洋 pacific ocean
vector<vector<bool>> can_reach_a=can_reach_p; //能到达大西洋
for(int i=0;i<m;++i){
dfs(heights,can_reach_p,i,0);
dfs(heights,can_reach_a,i,n-1);
}
for(int i=0;i<n;++i){
dfs(heights,can_reach_p,0,i);
dfs(heights,can_reach_a,m-1,i);
}
for(int i=0;i<m;++i){
for(int j=0;j<n;++j){
if(can_reach_a[i][j]&&can_reach_p[i][j]){
ret.push_back({
i,j});
}
}
}
return ret;
}
void dfs(const vector<vector<int>>& heights,
vector<vector<bool>> &can_reach,
int x,int y)
{
if(can_reach[x][y]==true) return;
can_reach[x][y]=true;
if(x-1>=0&&heights[x-1][y]>=heights[x][y])
dfs(heights,can_reach,x-1,y);
if(y-1>=0&&heights[x][y-1]>=heights[x][y])
dfs(heights,can_reach,x,y-1);
if(x+1<heights.size()&&heights[x+1][y]>=heights[x][y])
dfs(heights,can_reach,x+1,y);
if(y+1<heights[0].size()&&heights[x][y+1]>=heights[x][y])
dfs(heights,can_reach,x,y+1);
}
};
边栏推荐
- [Koltin Flow (2)] The end operator of the Flow operator
- MySQL模糊查询性能优化
- 2022年SQL大厂高频实战面试题(详细解析)
- 面试前需要巩固的算法知识点(自用,更新中)
- PyCharm usage tutorial (more detailed, picture + text)
- Learn FPGA from the underlying structure (6) ---- Distributed RAM (DRAM, Distributed RAM)
- 倒计数(来源:Google Kickstart2020 Round C Problem A)(DAY 88)
- 手把手教你彻底卸载MySQL
- Error: npm ERR code EPERM
- 使用DataEase开源工具制作一个高质量的数据大屏
猜你喜欢
What is SOA (Service Oriented Architecture)?
微信小程序开发学习
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
Different lower_case_table_names settings for server ('1') and data dictionary ('0') solution
Learn FPGA from the underlying structure (6) ---- Distributed RAM (DRAM, Distributed RAM)
131.分割回文串
PyCharm使用教程(较详细,图+文)
MySQL (2)
PyCharm usage tutorial (more detailed, picture + text)
2022年比若依更香的开源项目
随机推荐
131.分割回文串
2022年SQL经典面试题总结(带解析)
Countdown (Source: Google Kickstart2020 Round C Problem A) (DAY 88)
More fragrant open source projects than Ruoyi in 2022
ClickHouse 数据插入、更新与删除操作 SQL
create-nuxt-app创建出来的项目没有server
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
Graphic mirror symmetry (schematic diagram)
[GO Language Basics] 1. Why do I want to learn Golang and the popularization of GO language entry
Falling ants (Peking University entrance exam questions)
MySQL Soul 16 Questions, how many questions can you last?
Record Breaker (Google Kickstart2020 Round D Problem A)
2022年SQL大厂高频实战面试题(详细解析)
G巴士计数(Google Kickstart2014 Round D Problem B)(DAY 89)
MySQL 用户授权
[GLib] 什么是GType
ezTrack-master使用教程
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
Frobenius norm(Frobenius 范数)
mysql time field is set to current time by default