You are given a m x n 2D grid initialized with these three possible values.

  1. -1 - A wall or an obstacle.
  2. 0 - A gate.
  3. 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.

For example, given the 2D grid:

  1. INF - INF
  2. INF INF INF -
  3. INF - INF -
  4. - INF INF

After running your function, the 2D grid should be:

  1. -
  2. -
  3. - -
  4. -

思路:BSF。

  1. class Solution {
  2. public:
  3. void wallsAndGates(vector<vector<int>>& rooms) {
  4. const int inf = INT_MAX;
  5. queue<pair<int, int> > q;
  6. int height = rooms.size();
  7. int width = height > ? rooms[].size() : ;
  8. for (int i = ; i < height; i++)
  9. for (int j = ; j < width; j++)
  10. if (rooms[i][j] == ) q.push(make_pair(i, j));
  11. int rowChange[] = {, -, , };
  12. int colChange[] = {, , -, };
  13. while (!q.empty()) {
  14. pair<int, int> cur = q.front();
  15. q.pop();
  16. int row = get<>(cur), col = get<>(cur);
  17. for (int i = ; i < ; i++) {
  18. int nextRow = row + rowChange[i];
  19. int nextCol = col + colChange[i];
  20. if (nextRow > - && nextRow < height &&
  21. nextCol > - && nextCol < width && rooms[nextRow][nextCol] == inf) {
  22. rooms[nextRow][nextCol] = rooms[row][col] + ;
  23. q.push(make_pair(nextRow, nextCol));
  24. }
  25. }
  26. }
  27. }
  28. };

Walls and Gates -- LeetCode的更多相关文章

  1. 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的值. 最 ...

  2. [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 ...

  3. [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 ...

  4. LeetCode Walls and Gates

    原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...

  5. [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 ...

  6. LeetCode 286. Walls and Gates

    原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...

  7. 【LeetCode】286. Walls and Gates 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. PYTHON -MYSQLDB安装遇到的问题和解决办法

    目前下载的mysqldb在window下没有exe安装包了,只有源码. 使用python setup.py install 命令安装, 报错如下: 异常信息如下: F:\devtools\MySQL- ...

  2. Python IO关于mode参数的问题

    关于open()的mode参数: 'r':读 'w':写 'a':追加 'r+' == r+w(可读可写,文件若不存在就报错(IOError)) 'w+' == w+r(可读可写,文件若不存在就创建) ...

  3. SecureCRT自动登录跳板机/堡垒机并连接目标机器

    公司登录目标服务器,需要先登录跳板机(堡垒机),然后再登录目标机器,一共需要4.5步. MAC或LINUX机器可以写.SH脚本,那WINDOWS有没有一键登陆的方法呢? 常用的SecureCRT工具就 ...

  4. 【转载】总结使用Unity3D优化游戏运行性能的经验

    流畅的游戏玩法来自流畅的帧率,而我们即将推出的动作平台游戏<Shadow Blade>已经将在标准iPhone和iPad设备上实现每秒60帧视为一个重要目标. 以下是我们在紧凑的优化过程中 ...

  5. 课时5:闲聊之Python的数据类型

    目录: 一.引言 二.数据类型 >整型 >浮点型 >布尔类型 三.类型转换 四.获得关于类型的信息 五.课时05课后习题及答案 *********** 一.引言 ********** ...

  6. UVa 11806 - Cheerleaders (组合计数+容斥原理)

    <训练指南>p.108 #include <cstdio> #include <cstring> #include <cstdlib> using na ...

  7. C++ Programming with TDD之一:GMOCK框架简介

    所谓测试驱动开发,英文全称Test-Driven Development,简称TDD,是一种不同于传统软件开发流程的新型的开发方法.就是在明确要开发某个功能后,首先思考如何对这个功能进行测试,并完成测 ...

  8. [HNOI2015][bzoj4011] 落叶枫音 [拓扑DP]

    题面 传送门 思路 首先有一个结论(应该是有比较大的利用价值的): 有向无环图的生成外向树树个数等于所有入度非0的点的入度乘积 然后这道题里面,唯一不合拍的因素就是这里有一条可能成环的边 我们可以把这 ...

  9. vue-router中scrollBehavior的巧妙用法

    问题:使用keep-alive标签后部分安卓机返回缓存页位置不精确问题 解决方案: <div id="app"> <keep-alive> <rout ...

  10. bzoj1494【Noi2007】生成树计数

    题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1494 sol  :前排膜拜http://blog.csdn.net/qpswwww/artic ...