当前位置:网站首页>Pacific Atlantic current problem
Pacific Atlantic current problem
2022-07-23 14:16:00 【One 829】
There is one with The Pacific Ocean and The Atlantic ocean m x n Bordering rectangular islands . The Pacific Ocean Touch the left and upper edges of the island , The Atlantic ocean Touch the right and lower edges of the island .
The island is divided into square cells . Given a m x n Integer matrix heights, among heights[r][c] Represents the cell at the coordinates Altitude (r, c).
There is a lot of rainfall on the island , If the height of adjacent cells is less than or equal to The height of the current cell , The rain can go straight north 、 south 、 In the east 、 West flow to adjacent cells . Water can flow into the ocean from any cell adjacent to the ocean .
return Grid coordinates Of 2D list result, among Indicates that rainwater can flow from the cell The Pacific Ocean and The Atlantic ocean .result[i] = [ri, ci](ri, ci)
Example 1:

Input : Height = [[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]] Output : [[0,4],[1,3],[1,4],[2,2],[3,0],[3 ,1],[4,0]] Example 2:
Input : Height = [[2,1],[1,2]] Output : [[0,0],[0,1],[1,0],[1,1]] Error code
#include<iostream>
using namespace std;
int height[201][201], book[201][201];
int m, n,num=1;
// Current point coordinates
void dfs(int x, int y) {
int i, j, k, tx, ty;
// Define four directions
int next[4][2] = {
{0,1},{1,0},{0,-1},{-1,0}
};// Right down left up
// Try whether you can walk in four directions
for (k = 0;k < 4;k++) {
tx = x + next[k][0];
ty = y + next[k][1];
// If the right point is larger than the current point, it can flow through and then judge whether the next point meets the conditions , Find the right book All set
if (tx>=0 && ty>=0 && tx<n && ty<m &&height[tx][ty] >= height[x][y]&&book[tx][ty]==0) {
book[tx][ty] =1;
dfs(tx, ty);
}
}
return;
}
int main() {
int i, j, a[201][201] = { 0 }, b[201][201];
// Enter row and column
cin >> n >> m;
// input data
for (i = 0;i < n;i++)
for (j = 0;j < m;j++)
cin >> height[i][j];
// Points that meet the conditions enter the search ( The surrounding points are divided into four parts )
// On
for (i = 0;i < n;i++) {
book[0][i] = 1;
dfs(0, i);
}
// Left
for (i = 1;i < n;i++){
book[i][0] = 1;
dfs(i, 0);}
for (i = 0;i < n;i++)
for (j = 0;j < m;j++)
a[i][j] = book[i][j];// take book All passed in are assigned to a Array , The staging
// Empty book Tag array
for (i = 0;i < n;i++) {
for (j = 0;j < m;j++)
book[i][j] = { 0 };
}
cout << endl;
// Next
for (i = 1;i < n;i++) {
book[4][i] = 1;
dfs(n-1, i);
}
// Right
for (i = 1;i < n - 1;i++) {
book[i][4] = 1;
dfs(i, n-1);
}
// look for a and book Output the overlapped part of the array
for (i = 0;i < n;i++)
for (j = 0;j < m;j++)
{
if (a[i][j] == book[i][j])
cout << i << " " << j << endl;
}
return 0;
}
5 5
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
边栏推荐
- Pbootcms数据库转换教程(sqlite转mysql详细教程)
- Towhee 每周模型
- rtx3080相当于gtx什么显卡 rtx3080显卡什么水平 rtx3080显卡怎么样
- 天玑820相当于骁龙多少处理器 天玑1100相当于骁龙多少 天玑820怎么样
- How many processors is Tianji 720 equivalent to Xiaolong? How about Tianji 720 equivalent to Xiaolong
- rtx3080ti和rtx3080差距 3080和3080ti参数对比
- 酷睿i5 12490f和i5 12600k差距大吗
- LabVIEW运行中改变Chart的历史长度
- 背包问题详解
- antd form表单——重置方法不生效——基础积累——prop的重要性
猜你喜欢

MGRE comprehensive experiment
![[baiqihang] Niuer education helps colleges and universities visit enterprises, expand posts and promote employment](/img/41/6ef8f24732d9c75046ca8c55fd4841.png)
[baiqihang] Niuer education helps colleges and universities visit enterprises, expand posts and promote employment

Notes on the ninth day

鸡与蛋,产品与策略

回文相关题目

NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘

How many processors is Tianji 720 equivalent to Xiaolong? How about Tianji 720 equivalent to Xiaolong

LabVIEW运行中改变Chart的历史长度

BGP联邦实验

第六天笔记
随机推荐
【百企行】牛耳教育助力高校访企拓岗促就业专项行动
强化學習——策略梯度理解點
Design instantiation and connection
采样和数据驱动
Network security note 1 - Security of Internet Protocol
JS to implement encode64 encryption
MGRE comprehensive experiment
581. 最短无序连续子数组
200 lines of code, in-depth analysis of the principle and implementation of dynamic calculation diagram
shell跑的时候需要的需要了解命令
mysql开启定时调度任务执行
考研题库小程序中如何实现打开考研思维导图pdf
STM32输出SPWM波,HAL库,cubeMX配置,滤波后输出1KHz正弦波
激励发生器、监测器
网络安全笔记1——Internet协议的安全性
Day 10 notes
pingbanceshi
Rtx3080 is equivalent to GTX. What kind of graphics card is rtx3080? What level is rtx3080
中等靶场
设计例化和连接