Walls and Gates -- LeetCode
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 - INF
- INF INF INF -
- INF - INF -
- - INF INF
After running your function, the 2D grid should be:
- -
- -
- - -
- -
思路:BSF。
- class Solution {
- public:
- void wallsAndGates(vector<vector<int>>& rooms) {
- const int inf = INT_MAX;
- queue<pair<int, int> > q;
- int height = rooms.size();
- int width = height > ? rooms[].size() : ;
- for (int i = ; i < height; i++)
- for (int j = ; j < width; j++)
- if (rooms[i][j] == ) q.push(make_pair(i, j));
- int rowChange[] = {, -, , };
- int colChange[] = {, , -, };
- while (!q.empty()) {
- pair<int, int> cur = q.front();
- q.pop();
- int row = get<>(cur), col = get<>(cur);
- for (int i = ; i < ; i++) {
- int nextRow = row + rowChange[i];
- int nextCol = col + colChange[i];
- if (nextRow > - && nextRow < height &&
- nextCol > - && nextCol < width && rooms[nextRow][nextCol] == inf) {
- rooms[nextRow][nextCol] = rooms[row][col] + ;
- q.push(make_pair(nextRow, nextCol));
- }
- }
- }
- }
- };
Walls and Gates -- LeetCode的更多相关文章
- 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 ...
- [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 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 墙和门
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 ...
- [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 ...
随机推荐
- PYTHON -MYSQLDB安装遇到的问题和解决办法
目前下载的mysqldb在window下没有exe安装包了,只有源码. 使用python setup.py install 命令安装, 报错如下: 异常信息如下: F:\devtools\MySQL- ...
- Python IO关于mode参数的问题
关于open()的mode参数: 'r':读 'w':写 'a':追加 'r+' == r+w(可读可写,文件若不存在就报错(IOError)) 'w+' == w+r(可读可写,文件若不存在就创建) ...
- SecureCRT自动登录跳板机/堡垒机并连接目标机器
公司登录目标服务器,需要先登录跳板机(堡垒机),然后再登录目标机器,一共需要4.5步. MAC或LINUX机器可以写.SH脚本,那WINDOWS有没有一键登陆的方法呢? 常用的SecureCRT工具就 ...
- 【转载】总结使用Unity3D优化游戏运行性能的经验
流畅的游戏玩法来自流畅的帧率,而我们即将推出的动作平台游戏<Shadow Blade>已经将在标准iPhone和iPad设备上实现每秒60帧视为一个重要目标. 以下是我们在紧凑的优化过程中 ...
- 课时5:闲聊之Python的数据类型
目录: 一.引言 二.数据类型 >整型 >浮点型 >布尔类型 三.类型转换 四.获得关于类型的信息 五.课时05课后习题及答案 *********** 一.引言 ********** ...
- UVa 11806 - Cheerleaders (组合计数+容斥原理)
<训练指南>p.108 #include <cstdio> #include <cstring> #include <cstdlib> using na ...
- C++ Programming with TDD之一:GMOCK框架简介
所谓测试驱动开发,英文全称Test-Driven Development,简称TDD,是一种不同于传统软件开发流程的新型的开发方法.就是在明确要开发某个功能后,首先思考如何对这个功能进行测试,并完成测 ...
- [HNOI2015][bzoj4011] 落叶枫音 [拓扑DP]
题面 传送门 思路 首先有一个结论(应该是有比较大的利用价值的): 有向无环图的生成外向树树个数等于所有入度非0的点的入度乘积 然后这道题里面,唯一不合拍的因素就是这里有一条可能成环的边 我们可以把这 ...
- vue-router中scrollBehavior的巧妙用法
问题:使用keep-alive标签后部分安卓机返回缓存页位置不精确问题 解决方案: <div id="app"> <keep-alive> <rout ...
- bzoj1494【Noi2007】生成树计数
题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1494 sol :前排膜拜http://blog.csdn.net/qpswwww/artic ...