【LeetCode】286. Walls and Gates 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/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 value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.
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.
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
题目大意
你被给定一个 m × n 的二维网格,网格中有以下三种可能的初始化值:
- -1 表示墙或是障碍物
- 0 表示一扇门
- INF 无限表示一个空的房间。然后,我们用 231 - 1 = 2147483647 代表 INF。你可以认为通往门的距离总是小于 2147483647 的。
你要给每个空房间位上填上该房间到 最近 门的距离,如果无法到达门,则填 INF 即可。
解题方法
BFS
这个题可以使用BFS和DFS去解决,我选用的BFS。
- 找出所有为0的位置,放入队列中
- 从为0的位置开始向四个方向遍历,直接修改迷宫的可以走的位置的数字是距离门最近的距离
- 把新的可以走的位置放入队列中,继续搜索
C++代码如下:
class Solution {
public:
void wallsAndGates(vector<vector<int>>& rooms) {
if (rooms.empty() || rooms[0].empty()) return;
M = rooms.size();
N = rooms[0].size();
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (rooms[i][j] == 0) {
bfs(rooms, i, j);
}
}
}
}
void bfs(vector<vector<int>>& rooms, int x, int y) {
if (rooms[x][y] != 0) return;
queue<vector<int>> que;
que.push({x, y});
while (!que.empty()) {
vector<int> cur = que.front(); que.pop();
for (auto& dir : dirs) {
int newx = cur[0] + dir[0];
int newy = cur[1] + dir[1];
if (newx < 0 || newx >= M || newy < 0 || newy >= N
|| rooms[newx][newy] <= rooms[cur[0]][cur[1]])
continue;
rooms[newx][newy] = rooms[cur[0]][cur[1]] + 1;
que.push({newx, newy});
}
}
}
private:
int M, N;
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
};
日期
2019 年 9 月 23 日 —— 昨夜睡的早,错过了北京的烟火
【LeetCode】286. Walls and Gates 解题报告 (C++)的更多相关文章
- [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 286. Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
随机推荐
- Macbookpro快捷键
Alt键代替 Option 键,用 Windows 标志键代替 Command 键. 但是,使用的时候,Command健类似于Windows的Control键. 1.复制.粘贴.剪切.全选.查找 Co ...
- 35-Remove Element
Remove Element My Submissions QuestionEditorial Solution Total Accepted: 115367 Total Submissions: 3 ...
- kubernetes部署Docker私有仓库Registry
在后面的部署过程中,有很多的docker镜像文件,由于kubernetes是使用国外的镜像,可能会出现下载很慢或者下载不下来的情况,我们先搭建一个简单的镜像服务器,我们将需要的镜像下载回来,放到我们自 ...
- day34 前端基础之JavaScript
day34 前端基础之JavaScript ECMAScript 6 尽管 ECMAScript 是一个重要的标准,但它并不是 JavaScript 唯一的部分,当然,也不是唯一被标准化的部分.实际上 ...
- Rust 总章
1.1 Rust安装 3.5 Rust Generic Types, Traits, and Lifetimes 3.6 String 与 切片&str的区别 https://openslr. ...
- Shell脚本实现乱序排列文件内容的多种方法(洗牌问题)
洗牌问题:洗一副扑克,有什么好办法?既能洗得均匀,又能洗得快?即相对于一个文件来说怎样高效率的实现乱序排列? ChinaUnix 确实是 Shell 高手云集的地方,只要你想得到的问题,到那里基本上都 ...
- js调用高德地图API获取地理信息进行定位
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=(需要自 ...
- Anaconda+pycharm(jupyter lab)搭建环境
之前先是安装了pycharm,手动安装了python2.7和3.7版本,在pycharm里面使用alt+/手动下载包.后来想使用jupyter lab,手动下载包太麻烦且有版本管理的文艺,于是打算装A ...
- SpringBoot自定义控制层参数解析
一.背景 在Spring的Controller中,我们通过@RequestParam或@RequestBody就可以将请求中的参数映射到控制层具体的参数中,那么这个是怎么实现的呢?如果我现在控制层中的 ...
- C#编程概述(摘抄)
C#编程概述 一个简单的C#程序 标识符 标识符是一种字符串,用来命名变量.方法.参数和许多后面将要阐述的其他程序结构. 关键字 所有C#关键字都由小写字母组成,但是.NET类型名使用Pascal大小 ...