[LeetCode] 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 = 2147483647to representINFas 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] Walls and Gates 墙和门的更多相关文章
- [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 obstac ...
- LeetCode Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...
- 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】286. Walls and Gates 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- LeetCode 286. Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...
- Walls and Gates -- LeetCode
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- 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 ...
随机推荐
- 【分布式】Zookeeper会话
一.前言 前面分析了Zookeeper客户端的细节,接着继续学习Zookeeper中的一个非常重要的概念:会话. 二.会话 客户端与服务端之间任何交互操作都与会话息息相关,如临时节点的生命周期.客户端 ...
- 挣值管理 EVM
项目的挣值管理(Earned Value Management,EVM),是用与进度计划.成本预算和实际成本相联系的三个独立的变量,进行项目绩效测量的一种方法. 有三个比较重要的参数,用这三个参数能够 ...
- LinqToDB 源码分析——前言
记得笔者进入公司的时候接触的第一个ORM框架是Entity Framework.为了Entity Framework也看了不些的英文资料(不是笔者装B哦).正式使用三个月后.笔者对他有一个全面性的认识 ...
- 本博客现已迁移到chuxiuhong.com
欢迎大家访问,我会暂时保留这个博客的更新,实现两个博客的同步. 新博客地址: http://chuxiuhong.com
- 精彩 JavaScript 代码片段
1. 根据给定的条件在原有的数组上,得到所需要的新数组. ——<JavaScript 王者归来> var a = [-1,-1,1,2,-2,-2,-3,-3,3,-3]; functio ...
- Entity Framework Plus 系列目录
Entity Framework Plus 系列文章计划的已经全部写完,可能还有其他功能没有写到,希望大家能够多动手,尝试一下使用,一定会给您带来一些帮助的.文章全部写完,也应该出一个目录方便查看,目 ...
- 【移动前端开发实践】从无到有(统计、请求、MVC、模块化)H5开发须知
前言 不知不觉来百度已有半年之久,这半年是996的半年,是孤军奋战的半年,是跌跌撞撞的半年,一个字:真的是累死人啦! 我所进入的团队相当于公司内部创业团队,人员基本全部是新招的,最初开发时连数据库都没 ...
- arcgis 许可异常的解决
异常现象: arcgis 许可服务管理器中无法重新读取许可,许可服务启动后立即停止. 解决方法: 1.卸载license:安装新的license!重新破解,替换license文件夹BI ...
- JAVA-堆区,栈区,方法区。
转载:http://blog.csdn.net/wangxin1982314/article/details/50293241 堆区: 村线程操纵的数据(对象形式存放) 1 存储的全部是对象,每个对象 ...
- React Native class 后面的命名首字母需要大写
React Native class 后面的命名首字母需要大写 否则会报Expeted a component class,got [object Object].