当前位置:网站首页>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);
}
};
边栏推荐
- 分布式事务之 Seata框架的原理和实战使用(三)
- net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
- cnpm installation steps
- St. Regis Takeaway Project: New dishes and dishes paged query
- Teach you how to design a CSDN system
- MySQL模糊查询性能优化
- More fragrant open source projects than Ruoyi in 2022
- 手把手教你彻底卸载MySQL
- postman 请求 post 调用 传 复合 json数据
- Navicat new database
猜你喜欢

navicat无法连接mysql超详细处理方法

从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)

Error: npm ERR code EPERM

Navicat new database
![[Image processing] Image skeleton extraction based on central axis transformation with matlab code](/img/56/cfef9389291fa39612d55a07e1dbb3.png)
[Image processing] Image skeleton extraction based on central axis transformation with matlab code

爬虫数据是如何收集和整理的?

MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)

cnpm安装步骤

4、nerf(pytorch)

【线性神经网络】线性回归 / 基础优化方法
随机推荐
MySQL (2)
This dependency was not found:
ezTrack-master使用教程
cnpm installation steps
每日练习------输出一个整数的二进制数、八进制数、十六进制数。
留念 · 大学时代最后的系统设计图
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
安装Nuxt.js时出现错误:TypeError:Cannot read property ‘eslint‘ of undefined
MySql模糊查询大全
4、nerf(pytorch)
解决没有配置本地nacos但是一直发生localhost8848连接异常的问题
微信小程序开发学习
2022年比若依更香的开源项目
[GO Language Basics] 1. Why do I want to learn Golang and the popularization of GO language entry
4461. 范围分区(Google Kickstart2022 Round C Problem B)
MySQL的存储过程
MySql的初识感悟,以及sql语句中的DDL和DML和DQL的基本语法
【Koltin Flow(二)】Flow操作符之末端操作符
从底层结构开始学习FPGA(6)----分布式RAM(DRAM,Distributed RAM)
G Bus Count (Google Kickstart2014 Round D Problem B) (DAY 89)