Given a N*N matrix contains lakes, each lake is represented by an elevation. The water in each lake can flow to its neighbours which has lower or equal elevations.
Suppose the left and top side of the matrix is surrounded by Pacific, the right and bottom is Atlantic.
Please write a function and return all lakes that can flow to both Pacific and Atlantic.
For example:
Pacific: ~ Atlantic: * ~ ~ ~ ~ ~ ~ ~ ~ 1 2 2 3 (5) * ~ 3 2 3 (4) (4) * ~ 2 4 (5) 3 1 * ~ (6) (7) 1 4 5 * ~ (5) 1 1 2 4 * * * * * * * *
The elements in parentheses are expected outputs:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]]
/* struct Point { int x{ 0 }, y{ 0 }; }; */ vector<vector<int>> dirs = {{0,1}, {0,-1}, {1,0}, {-1,0}}; void dfs(vector<vector<int>>& mat, unordered_set<Point>& ocean, Point p) { int n = mat.size(); ocean.insert(p); for(auto& dir:dirs) { int x = p.x+dir[0]; int y = p.y+dir[1]; if(x<0 || y<0 || x>=n || y>=n || mat[x][y]<mat[p.x][p.y] || ocean.count({x,y})) continue; dfs(mat, ocean, {x,y}); } } vector<Point> flowing_water(vector<vector<int>>& mat) { unordered_set<Point> pac, atl; int n = mat.size(); for(int i=0; i<n; i++) { dfs(mat, pac, {0, i}); dfs(mat, pac, {i, 0}); dfs(mat, atl, {n-1, i}); dfs(mat, atl, {i, n-1}); } vector<Point> res; // unordered results // for(auto& p:atl) { // if(pac.count(p)) { // res.push_back(p); // } // } // ordered results for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { Point p = {i, j}; if(pac.count(p) && atl.count(p)) { res.push_back(p); } } } return res; }
相关推荐
标题中的“32-bit-super-flowing-water-light.zip”指的是一个包含32位超级流水灯程序的压缩文件。"32流水灯"是这个程序的核心功能,它是一种LED灯光效果,通常在电子工程和嵌入式系统中使用,用于创建视觉上的流动...
React 项目目录一、二、三、四、五、六、七、八、Redux-saga状态共享九、结尾十、Github链接一、技术栈详情可参阅 package.jsonYarnReact 16.9.0Redux-sagaReact RouterAxios 请求库Webpack 4.0+ES6 + BabelAntd项目...
We use 1650-nm femtosecond laser pulses to induce the plasma in a stable free-flowing water film under the strong field ionization mechanism. Moreover, we adopt intense terahertz (THz) pulses to ...
Terahertz (THz) waves could be generated through exciting a gravity-guided, free-flowing water wedge by a dual-color pulse. It is not required to rotate the optimal angle considering the water film as...
流式计算的基础和原理 Streaming Data introduces the concepts and requirements of ... The book is an idea-rich tutorial that teaches you to think about how to efficiently interact with fast-flowing data.
而“Flowing and Fusing”是针对MOT的一种创新方法,结合了运动分析和数据融合策略,尤其在利用无线通信如WiFi信号进行跟踪时,展现出独特的优势。 WiFi信号在MOT中的应用源于其独特的特性:无线电磁波能够穿透墙壁...
在对标题“论文研究-Information Asymmetry Flowing in Complex Network.pdf”以及内容摘要进行解读后,可以归纳出以下知识点: 首先,本文探讨了复杂网络中的信息不对称性流动问题,这是基于经济学中的信息不对称...
About the book Streaming Data is an idea-rich tutorial that teaches you to think about efficiently interacting with fast-flowing data. Through relevant examples and illustrated use cases, you'll ...
遗传学、基因组学、蛋白质组学和生物信息学百科全书 4000页 彩色PDF清晰版。 作者: Jorde, Lynn B. (EDT) 出版社: John Wiley & Sons Inc 出版年: 2005-11 ...flowing in discovery." ELECTRIC REVIEW
~~ B O O K R E V I E W S GILBERT R. GREDLER University of South ... It is difficult to write a smooth-flowing text when so many individuals are involved, but overall this objective has been nicel
~~ B O O K R E V I E W S GILBERT R. GREDLER University of South ... It is difficult to write a smooth-flowing text when so many individuals are involved, but overall this objective has been nicel
完整英文电子版 IEC 60068-2-60:2015 Environmental testing - Part 2-60:Tests - Test Ke:Flowing mixed gas corrosion test(环境测试--第2-60部分:测试--测试Ke:流动的混合气体腐蚀测试)。IEC 60068-2-60:...
- 自由流动交通(Free-flowing traffic):指同一方向上、同一路面上移动的车辆; - 时间间隙(Time gap):根据车辆速度和净空计算出的值,用于表示本车与前车之间的时间差距。 ACC系统的性能和功能的实现,依赖于...
在"Shop Floor--Flowing v03.11"这个源码包中,我们可以看到该程序的最新版本。版本号v03.11可能代表了此系统的第三次重大更新,且包含了性能优化和功能改进。源码中的各个文件可能包括了主程序、数据库连接模块、...
Hunted - A Flowing Editorial Magazine Theme 猎杀 - 流动的编辑杂志主题" ---------- 泰森云每天更新发布最新WordPress主题、HTML主题、WordPress插件、shopify主题、opencart主题、PHP项目源码、安卓项目源码、...
纤维悬浮湍流收缩流场中的取向分布和流变特性计算涉及复杂的流体动力学和数学建模问题。在造纸工业中,由于纸张是由稀释的纤维悬浮液在纸机开始时通过特制的管道形成的,故此问题尤为重要。本文探讨了纤维悬浮液在...