[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 ...
随机推荐
- 使用k8s-prometheus-adapter实现HPA
环境: kubernetes 1.11+/openshift3.11 自定义metric HPA原理: 首选需要注册一个apiservice(custom metrics API). 当HPA请求me ...
- SEO-------- 了解
SEO(Search Engine Optimization) 译为:搜索引擎优化,是一种透过了解搜索引擎的运作规则来调整网站,以及提高目的的网站在有关搜索引擎内的排名方式. 目的:为了让用户更快的搜 ...
- Python urlib 模块
Python urlib 模块 urlib 模块 当前再爬虫领域使用的比较少,不过它对图片爬取处理会比较方便.这里我们只使用它的图片爬取. 使用 urlib.request.urlretrieve(u ...
- Kubernetes Pod 资源限制
Kubernetes Pod 资源限制 官方文档:https://kubernetes.io/docs/concepts/configuration/manage-compute-resources- ...
- Mybatis中的Mapper.xml映射文件sql查询接收多个参数
我们都知道,在Mybatis中的Mapper.xml映射文件可以定制动态SQL,在dao层定义的接口中定义的参数传到xml文件中之后,在查询之前mybatis会对其进行动态解析,通常使用#{}接收 ...
- JVM的监控工具之jinfo
参考博客:https://www.jianshu.com/p/8d8aef212b25 jinfo(ConfigurationInfoforJava)的作用是实时地查看和调整虚拟机各项参数,使用jps ...
- Python 学习 第15篇:日期和时间
datetime模块中包含五种基本类型:date.time.datetime.timedelta和tzinfo,tz是time zone的缩写,tzinfo用于表示时区信息. 一,date类型 dat ...
- RSA加密方法
/// <summary> /// RSA加密 /// </summary> /// <param name="dat ...
- Java 并发学习总结
目录 基础篇 进阶篇 并发编程的的三个概念(特性)? JMM(Java 内存模型) volatile 关键字 1. Java 内存模型(为什么要有 volatile) 2. volatile 原理 追 ...
- SAP 如何得到交货单上的序列号清单?
SAP 如何得到交货单上的序列号清单? 以内向交货单为例(外向交货单方法了类似)予以说明. 1)VL33N,在交货单显示界面, 但是没办法通过这个界面里导出序列号清单. 2),只能通过查表的方式导出序 ...