[LeetCode] 286. Walls and Gates 墙和门
You are given a m x n 2D grid initialized with these three possible values.
-1
- A wall or an obstacle.0
- A gate.INF
- Infinity means an empty room. We use the value231 - 1 = 2147483647
to representINF
as you may assume that the distance to a gate is less than2147483647
.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF
.
For example, given the 2D grid:
INF -1 0 INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
After running your function, the 2D grid should be:
3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4
这道题类似一种迷宫问题,规定了 -1 表示墙,0表示门,让求每个点到门的最近的曼哈顿距离,这其实类似于求距离场 Distance Map 的问题,那么先考虑用 DFS 来解,思路是,搜索0的位置,每找到一个0,以其周围四个相邻点为起点,开始 DFS 遍历,并带入深度值1,如果遇到的值大于当前深度值,将位置值赋为当前深度值,并对当前点的四个相邻点开始DFS遍历,注意此时深度值需要加1,这样遍历完成后,所有的位置就被正确地更新了,参见代码如下:
解法一:
class Solution {
public:
void wallsAndGates(vector<vector<int>>& rooms) {
for (int i = ; i < rooms.size(); ++i) {
for (int j = ; j < rooms[i].size(); ++j) {
if (rooms[i][j] == ) dfs(rooms, i, j, );
}
}
}
void dfs(vector<vector<int>>& rooms, int i, int j, int val) {
if (i < || i >= rooms.size() || j < || j >= rooms[i].size() || rooms[i][j] < val) return;
rooms[i][j] = val;
dfs(rooms, i + , j, val + );
dfs(rooms, i - , j, val + );
dfs(rooms, i, j + , val + );
dfs(rooms, i, j - , val + );
}
};
那么下面再来看 BFS 的解法,需要借助 queue,首先把门的位置都排入 queue 中,然后开始循环,对于门位置的四个相邻点,判断其是否在矩阵范围内,并且位置值是否大于上一位置的值加1,如果满足这些条件,将当前位置赋为上一位置加1,并将次位置排入 queue 中,这样等 queue 中的元素遍历完了,所有位置的值就被正确地更新了,参见代码如下:
解法二:
class Solution {
public:
void wallsAndGates(vector<vector<int>>& rooms) {
queue<pair<int, int>> q;
vector<vector<int>> dirs{{, -}, {-, }, {, }, {, }};
for (int i = ; i < rooms.size(); ++i) {
for (int j = ; j < rooms[i].size(); ++j) {
if (rooms[i][j] == ) q.push({i, j});
}
}
while (!q.empty()) {
int i = q.front().first, j = q.front().second; q.pop();
for (int k = ; k < dirs.size(); ++k) {
int x = i + dirs[k][], y = j + dirs[k][];
if (x < || x >= rooms.size() || y < || y >= rooms[].size() || rooms[x][y] < rooms[i][j] + ) continue;
rooms[x][y] = rooms[i][j] + ;
q.push({x, y});
}
}
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/286
类似题目:
Shortest Distance from All Buildings
Rotting Oranges
参考资料:
https://leetcode.com/problems/walls-and-gates/
https://leetcode.com/problems/walls-and-gates/discuss/72745/Java-BFS-Solution-O(mn)-Time
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 286. Walls and Gates 墙和门的更多相关文章
- [LeetCode] Walls and Gates 墙和门
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- LeetCode 286. Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...
- 【LeetCode】286. Walls and Gates 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 286. Walls and Gates
题目: You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an ob ...
- [LeetCode] 286. Walls and Gates_Medium tag: BFS
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings
542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...
- [Locked] Walls and Gates
Walls and Gates You are given a m x n 2D grid initialized with these three possible values. -1 - A w ...
- [Swift]LeetCode286. 墙和门 $ Walls and Gates
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- LeetCode Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...
随机推荐
- tensor 中mul_,add_解读
pytorch 中文网文档链接 https://ptorch.com/docs/1/Tensor 每一个张量tensor都有一个相应的torch.Storage保存其数据,张量类提供了一个多维的,横向 ...
- 【测试方法】Web测试中bug定位基本方法
知识总结:Web测试中bug定位基本方法 涉及知识点:测试方法 在web测试过程中,经常会遇到页面中内容或数据显示错误,甚至不显示,第一反应就是BUG,没错,确实是BUG.进一步了解这个BUG的问题出 ...
- appstore-react v2.0—redux-actions和redux-saga的应用
开发文档 https://redux-saga.js.org/ https://redux-saga-in-chinese.js.org/ https://redux-actions.js.org/ ...
- Python基础24
import 与 from import 知乎上说的简洁明了,zhihu.com/question/38857862 from import, 导入之后就能拿来用了,直接用!到处用!
- Python基础18
“为什么有列表,还要元组?” 1. 元组可看成是简单的对象组合,而列表是随时间改变的数据集合. 2. 元组的不可变特性提供了某种完整性,确保元组不会被另一个引用来修改.类似于其它语言中的常数声明.
- .Net网站防盗链
一.简单写法,在Global.asax下的这个方法中添加如下信息 /// <summary> /// 管道当中的第一个事件 /// </summary> /// <par ...
- vs扩展和更新插件的开发
一.调试 以 MinimalisticView.vsix (https://github.com/poma/MinimalisticView) 为例. 正如 | Marketplace 上介绍的,这个 ...
- 设计模式:单例模式(singleton)
singleton模式属于创建型设计模式.其作用是在程序设计中,对于某一个类而言,全局只能存在一个实例对象. 下面以C++为例,对单例模式进行说明: 1. 最基本单例模式(单线程) class Sin ...
- linux下各文件夹的结构说明及用途介绍:Linux目录结构介绍
linux下各文件夹的结构说明及用途介绍: /bin:二进制可执行命令. /dev:设备特殊文件. /etc:系统管理和配置文件. /etc/rc.d:启动的配 置文件和脚本. /home:用户主目录 ...
- 两种方式实现浅拷贝 for in实现和Object.assign({}, 对象1, 对象2);
浅拷贝只拷贝对象的一层,如果对象的属性还是对象,那么user3和user4这两个对象对应的值都会发生改变 // 拷贝分为浅拷贝和深拷贝. // 浅拷贝的实现 通过for in实现 var user1 ...