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. Python 爬虫-豆瓣读书

    import requests from bs4 import BeautifulSoup def parse_html(num): headers = { 'User-Agent': 'Mozill ...

  2. sql的nvl()函数

    一NVL函数是一个空值转换函数 NVL(表达式1,表达式2) 如果表达式1为空值,NVL返回值为表达式2的值,否则返回表达式1的值. 该函数的目的是把一个空值(null)转换成一个实际的值.其表达式的 ...

  3. 孤荷凌寒自学python第二十一天初识python的类

    孤荷凌寒自学python第二十一天初识python的类 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 类是面向对象的编程语言非常重要的概念. 编程语言的进化史中从顺序编程到结构化编程,最后才 ...

  4. 《大道至简》第一章 编程的精义 java伪代码形式

    愚公.这位名家身上,浓缩了项目组织者.团队经理.编程人员.技术分析师等众多角色的优秀素质. 愚公移山事件分析: 原始需求:惩山北之塞,出入之迂 项目沟通方式:聚室而某曰 项目目标:毕力平险,指通豫南, ...

  5. 【操作系统】关于C语言设计程序退出自动关闭窗口的问题

    有些同学在做实验一 命令解释程序的编写的时候,输入quit命令退出程序,窗口并没有关闭,如下图所示需要Press any key to continue(按任意键)之后才关闭. 出现这个结果的原因是在 ...

  6. XML快速入门

    XML是什么 Extensible Markup Language 自定义标签: 用来传输数据: 可扩展标记语言,是一种类似超文本标记语言的标记语言. 与HTML的比较: 1.不是用来替代HTML的: ...

  7. BZOJ 1731: [Usaco2005 dec]Layout 排队布局

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  8. redis常用监控命令

    redis常用监控命令 1.实时监控redis服务收到来自应用的所有命令 1 2 3 4 5 6 7 redis-cli   127.0.0.1:6379>monitor   150996415 ...

  9. [AT2698] Don't Be a Subsequence

    题目大意:给定一个字符串,求一个最短的串要求没有在该字符串的子串中出现过,如果有多个,输出字典序最小的那一个. 题解:倒着跑一遍原字符串(以下编号为$1\sim n$),按出现了所有$26$个字母来分 ...

  10. [poj] 3090 Visible Lattice Points

    原题 欧拉函数 我们发现,对于每一个斜率来说,这条直线上的点,只有gcd(x,y)=1时可行,所以求欧拉函数的前缀和.2*f[n]+1即为答案. #include<cstdio> #def ...