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:

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的更多相关文章

  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. ADB push 和ADB pull命令

    adb push命令 :从电脑上传送文件到手机: adb pull命令 :从手机传送文件到电脑上      

  2. Python+Selenium框架设计篇之-什么是POM

    前面我们介绍了Python中的单元测试框架unittest,以后我们所有的测试类文件,都采用unittest来辅助我们进行debug和脚本开发.搞定了debug机制和确定了unittest来进行创建和 ...

  3. 孤荷凌寒自学python第五天初识python的列表

    孤荷凌寒自学python第五天 列表 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 粗俗地区分列表,可以这样理解,定义或print列表后显示时,列表中的各元素都是用一个方括号[]括起来的. ...

  4. [C++] 数据结构应用——链表

    C++ 数据结构应用--链表 代码已经封装成class啦,方便使用. 头文件:Linklist.h #include <iostream> /*********************** ...

  5. NYOJ36 水池数目

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  6. 16个简单实用的.htaccess技巧

    .htaccess 文件 (Hypertext Access file) 是Apache Web服务器的一个非常强大的配置文件,对于这个文件,Apache有一堆参数可以让你配置出几乎随心所欲的功能.. ...

  7. Linux命令 -磁盘和文件系统类

    声明:本文所涉及到的Linux命令均为最常见的用法,未列举之参数,自行查阅man 1.df 磁盘容量 -h 以人类易读方式展示(GB.KB)等 df -h /usr 2.du 文件或目录的容量 -s ...

  8. POJ 2891 Strange Way to Express Integers | exGcd解同余方程组

    题面就是让你解同余方程组(模数不互质) 题解: 先考虑一下两个方程 x=r1 mod(m1) x=r2 mod (m2) 去掉mod x=r1+m1y1   ......1 x=r2+m2y2   . ...

  9. BZOJ5157 [Tjoi2014]上升子序列 【树状数组】

    题目链接 BZOJ5157 题解 我们只需计算每个位置为开头产生的贡献大小,就相当于之后每个大于当前位置的位置产生的贡献 + 1之和 离散化后用树状数组维护即可 要注意去重,后面计算的包含之前的,记录 ...

  10. BZOJ1196 [HNOI2006]公路修建问题 【二分 + Kruskal】

    题目 OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Association组织成立了,旨在建立O ...