当前位置:网站首页>太平洋大西洋水流问题
太平洋大西洋水流问题
2022-07-23 08:00:00 【一只829】
有一个与太平洋和大西洋m x n接壤的长方形岛屿。太平洋接触岛屿的左边缘和上边缘,大西洋接触岛屿的右边缘和下边缘。
该岛被划分为方形单元格。给定一个m x n整数矩阵heights,其中heights[r][c]表示坐标处单元格的海拔高度(r, c)。
岛上降雨量很大,如果相邻单元格的高度小于或等于当前单元格的高度,雨水可以直接向北、南、东、西方向流向相邻的单元格。水可以从与海洋相邻的任何细胞流入海洋。
返回网格坐标的2D 列表result,其中表示雨水可以从单元格流向太平洋和大西洋。result[i] = [ri, ci](ri, ci)
示例 1:

输入:高度 = [[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]] 输出: [[0,4],[1,3],[1,4],[2,2],[3,0],[3 ,1],[4,0]] 示例 2:
输入:高度 = [[2,1],[1,2]] 输出: [[0,0],[0,1],[1,0],[1,1]] 错误代码
#include<iostream>
using namespace std;
int height[201][201], book[201][201];
int m, n,num=1;
//当前点坐标
void dfs(int x, int y) {
int i, j, k, tx, ty;
//定义四个方向
int next[4][2] = {
{0,1},{1,0},{0,-1},{-1,0}
};//右下左上
//尝试四个方向是否能走
for (k = 0;k < 4;k++) {
tx = x + next[k][0];
ty = y + next[k][1];
//如果右边点大于当前点则可以流经然后再判断下一个点是否满足条件,找到满足条件的book都置
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];
//输入行列
cin >> n >> m;
//输入数据
for (i = 0;i < n;i++)
for (j = 0;j < m;j++)
cin >> height[i][j];
//满足条件的点进入搜索(四周的点分为四个部分)
//上
for (i = 0;i < n;i++) {
book[0][i] = 1;
dfs(0, i);
}
//左
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];//将book中走过的全部赋值给a数组,暂存
//清空book标记数组
for (i = 0;i < n;i++) {
for (j = 0;j < m;j++)
book[i][j] = { 0 };
}
cout << endl;
//下
for (i = 1;i < n;i++) {
book[4][i] = 1;
dfs(n-1, i);
}
//右
for (i = 1;i < n - 1;i++) {
book[i][4] = 1;
dfs(i, n-1);
}
//找a和book数组重合的部分进行输出
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
边栏推荐
- 使用Stream流来进行分类展示。
- 静态综合实验(HCIA)
- Canvas eraser function
- Day 8 notes
- mysql开启定时调度任务执行
- Notes on key vocabulary of the original English book biography of jobs (15) [chapter four]
- 第九天笔记
- js 实现 encode64 加密
- Day 12 notes
- [understanding of opportunity-50]: Guiguzi - the twelfth Rune chapter - the art of being a good leader: keep your position, observe the four directions, cave in danger, talk widely, empty advice, set
猜你喜欢

Talent evaluation Core i7 12850hx and i7 12700h which to choose

What level is rtx3090ti? What level is rtx3090ti graphics card? How about rtx3090ti graphics card

Google Earth engine -- a small bug in gee. Images of transcontinental boundaries cannot be obtained

【微信小程序】案例 - 本地生活

NR Modulation 5

MySQL enables scheduled task execution

MYSQL练习题:向CEO汇报的所有员工

Comparison of iqoo 10 pro and Xiaomi 12 ultra configurations

iQOO 10 Pro和vivo X80 Pro区别 哪个好详细参数配置对比

Ansible first knowledge of learning one
随机推荐
SDF refraction and reflection effect recording
Notes on the fourth day
天玑820相当于骁龙多少处理器 天玑1100相当于骁龙多少 天玑820怎么样
How to judge whether an object is empty
[laser principle and application -7]: semiconductor refrigeration sheet and Tec thermostat
酷睿i5 12490f和i5 12600k差距大吗
Pbootcms数据库转换教程(sqlite转mysql详细教程)
达人评测酷睿i7 12850hx和i7 12700h选哪个
强化學習——策略梯度理解點
Golang remote server debugging
背包问题详解
第五天实验
Static comprehensive experiment (HCIA)
Day 10 notes
英特尔赛扬7305性能怎么样?相当于什么水平级别
设计例化和连接
pingbanceshi
MYSQL练习题:向CEO汇报的所有员工
How about the performance of Ruilong R7 Pro 5875u? What level is it equivalent to
Creo 9.0 如何快速修改CAD坐标系?