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 3基础教程10-全局变量和局部变量

    本文来讲讲全局变量和局部变量,前面学习了函数的基本使用,所以,这里就要注意变量的使用和访问权限. 试试下面的demo.py

  2. Appium与python自动测试环境及demo详解

    App--UI自动化这种高端的名词已经被越来越多的人所高呼,可是从实际角度来讲,个人觉得还是有点鸡肋,不如接口自动化敏捷度高,工作量 也是接口自动化的好几倍.但是,[划重点了]  在技术时代中,作为测 ...

  3. (原)Unreal渲染模块 管线 - 程序和场景查询

    @author: 白袍小道 查看随意,转载随缘     第一部分: 这里主要关心加速算法,和该阶段相关的UE模块的结构和组件的处理. What-HOW-Why-HOW-What(嘿嘿,老规矩) 1.渲 ...

  4. Python MySQLdb 模块使用方法

    import MySQLdb 2.和数据库建立连接 conn=MySQLdb.connect(host="localhost",user="root",pass ...

  5. 在jsp页面中使用jstl标签

    第一步:引入标签库 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%> 第 ...

  6. WinDbg分析dump文件排查bug

    文章:WinDbg-如何抓取dump文件 命令: cd C:\Windows\System32\inetsrv appcmd list wp 可以查看各个站点的pid

  7. c#中onclick事件请求的两种区别

    在C#中如果是asp控件的button有两个click的调用,一个是OnClick,一个是OnClientClick.那么这两者有什么区别呢,下面就来说说区别. <asp:Button ID=& ...

  8. PHP 自制分页类

    思路: 通过给页面url传递get参数,来控制每页的sql查询(mysql关键词:limit),实现分页查询 代码: class getpage{ public $pagenum; public $p ...

  9. 【BZOJ 5048 塌陷的牧场】

    Time Limit: 25 Sec  Memory Limit: 256 MBSubmit: 77  Solved: 34[Submit][Status][Discuss] Description ...

  10. js常用数组去重

    // ES6 function unique (arr){ const seen = new Map() return arr.filter((a) => !seen.has(a) && ...