当前位置:网站首页>LeetCode1765. Highest point in the map (BFS)
LeetCode1765. Highest point in the map (BFS)
2022-04-21 18:50:00 【GSX_ MI】
Their thinking :
1. Find the location of the water , The team , Bring out the height of the first floor .
2. Any adjacent grid height difference at most by 1, Bring out the height of adjacent grids through the current node , Then team up the adjacent squares ;
3. Continue this process until the queue is empty ; That is, from point to surface .
Code :
class Solution
{
public:
int dir[4][2] = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
vector<vector<int>> highestPeak(vector<vector<int>> &isWater)
{
int row = isWater.size();
int col = isWater[0].size();
vector<vector<int>> ans(row, vector<int>(col, -1)); // -1 Indicates that the grid has not been accessed
queue<pair<int, int>> q; // Secondary queue
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
if (isWater[i][j])
{
ans[i][j] = 0;
q.push(make_pair(i,j)); // Team up all waters
}
}
}
while (!q.empty())
{
int sz = q.size();
while(sz--)
{
int t_row = q.front().first;
int t_col = q.front().second;
q.pop();
for (int i = 0; i < 4; ++i) // Four directions
{
int new_row = t_row + dir[i][0];
int new_col = t_col + dir[i][1];
if (new_row < 0 || new_row >= row || new_col < 0 || new_col >= col
||ans[new_row][new_col] != -1)
{
continue; // If the position is out of bounds , Visited , Then skip
}
ans[new_row][new_col] = ans[t_row][t_col] + 1;
q.push(make_pair(new_row,new_col));
}
}
}
return ans;
}
};
版权声明
本文为[GSX_ MI]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211849176308.html
边栏推荐
- [04] [02] [02] SPI mechanism
- 【js学习笔记四十】复杂工厂模式
- OBS access network camera
- ViewPager中Fragment状态保存的哪些事
- SQL 数据类型
- Understand the new economic model of platofarm and its ecological progress
- The central bank defines the key points of payment supervision in 2022, and all platform enterprises should pay attention to clearing risks
- CVPR2022 Oral | CosFace、ArcFace的大统一升级,AdaFace解决低质量图像人脸识
- URL transcoding problem: urlcoder Decode (STR) is obsolete. Solution: decode (string s, string ENC) throws unsupportedencodingexceptio
- 【持续更新中】C#常见问题及其解决(VS2019)
猜你喜欢

win10 uwp 访问解决方案文件

What kind of Bluetooth headset is inexpensive and practical? Wireless Bluetooth headset for student party

无线蓝牙耳机哪款比较好用?2022蓝牙耳机推荐

This thing is called a jump watch?

为什么你做数据分析没思路?

毕业三年,一事无成,被迫回老家,一个决定改变一生。

Tencent cloud database tdsql -- blog database migration practice

有什么蓝牙耳机不贵又实用?适合学生党的无线蓝牙耳机

Pictures and texts teach you how to generate code with one click in idea to improve development efficiency!

CVPR2022 Oral | CosFace、ArcFace的大统一升级,AdaFace解决低质量图像人脸识
随机推荐
Does qiniu business school need money to open a securities account? Is it safe?
如何查看redis源码中的 zskiplist 结构
腾讯云数据库TDSQL——博客数据库迁移实践
ssh-keygen 设置免密登录
DX12渲染引擎目录
【无标题】
一文读懂PlatoFarm新经济模型以及生态进展
无线蓝牙耳机哪款比较好用?2022蓝牙耳机推荐
[04][02][02] SPI 机制
原生JS实现FlappyBird游戏 超详细解析
Kotlin | 关于 Lazy ,你应该了解的这些事
OBS接入网络摄像机
毕业三年,一事无成,被迫回老家,一个决定改变一生。
Win10 uwp asynchronous progress bar
Major event review Carnival link construction
How to detect how many cameras are plugged into the PC? And conduct multi camera synchronous recording?
Tencent cloud database tdsql -- blog database migration practice
Daily question series: water bottle
How to view the zskiplist structure in redis source code
VB 判断某数是否是素数。