[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 ...
随机推荐
- Apache Maven 3.6.3配置安装
1.maven 3.5 下载地址:http://maven.apache.org/download.cgi 2.下载了解压到 3.配置环境变量 4.测试看是否安装成功 5.maven配置(全局配置,用 ...
- 一些优秀的 Entity Framework 开源项目
增加全局过滤的项目(比如:IsDeleted 字段):https://github.com/zzzprojects/EntityFramework.DynamicFilters 谢谢浏览!
- etcd 开启auth认证
一.概述 1.etcd的v2和v3的认证有些不同,需要分别设置2.Etcd通过用户(user)-角色(role)-权限的方式来控制访问,用户关联角色,角色拥有权限,从而用户也就拥有了相应的权限3.Et ...
- CentOS7系统yum方式安装MySQL5.7
参考:https://www.cnblogs.com/bigbrotherer/p/7241845.html#top 1.在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要, ...
- 对try catch finally的理解
对try catch finally的理解1.finally 总是会运行的,即使在catch中thorw抛出异常了. 2.finally 在 return后没有结束,而是继续运行finally 2. ...
- Visual Studio中的主题定制变得更加容易
有时Visual Studio的默认主题是不够的.幸运的是,我们刚刚重新设计了创建和导入自定义主题的过程. 导入主题的唯一方法之一是下载旧的Color Theme Editor扩展.如果你足够勇敢地创 ...
- LiveBOS Webservice传参类型为list数组
昨天有使用soap传输数据到Webservice,其中字符串类型的都已经传输成功,但是有几个参数传输失败,java服务器端收到的空值. 因为我是php的,然后接收端是java制作的,其中有几个参数是l ...
- python中class的总结
思维导图文件:https://files-cdn.cnblogs.com/files/benjieming/class%E6%9C%BA%E5%88%B6.zip
- 实验6:Mapreduce实例——WordCount
实验目的1.准确理解Mapreduce的设计原理2.熟练掌握WordCount程序代码编写3.学会自己编写WordCount程序进行词频统计实验原理MapReduce采用的是“分而治之”的 ...
- CSS常用布局方式-两列布局、三列布局
CSS基础 2.几种布局方式1)table布局 当年主流的布局方式,第一种是通过table tr td布局 示例: <style type="text/css"> ta ...