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 = 2147483647to representINFas 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 ...
随机推荐
- 1155 Heap Paths (30 分)(堆+dfs遍历)
比较简单的一题 遍历左右的时候注意一下 #include<bits/stdc++.h> using namespace std; ; ]; ; vector<int>t; ve ...
- Extjs msgTarget 提示位置
extjs msgTarget 有效值包括: qtip:显示一个浮动的提示消息 title:显示一个浏览器浮动提示消息 under:在字段下面显示一个提示消息,使用under时要注意表单的高度 sid ...
- Limeng:Individual Project: Word frequency program -BUAA Advanced Software Engineering
11061190-李孟 Implement a console application to tally the frequency of words under a directory (2 mod ...
- java作业 2017.10.14
课后作业一 1.设计思想: (1)通过组合数公式计算:分别输入中的n和k的值.定义一个计算n!的方法,然后调用方法分别计算出n!,k!,(n-k)!,然后通过公式=n!/(k!*(n-k)!)算出的值 ...
- CentOS程序 开机启动设置与chkconfig命令学习
CentOS设置程序开机启动的方法: 1.启动命令添加到/etc/rc.d/rc.local 文件中, 如: vim /etc/rc.d/rc.local #!/bin/sh # # This scr ...
- BZOJ 1014 [JSOI2008]火星人prefix | Splay维护哈希值
题目: 题解: #include<cstdio> #include<algorithm> #include<cstring> typedef long long l ...
- POJ 1236 Networks of School Tarjan 基础
题目大意: 给一个有向图,一个文件可以从某个点出发传递向他能连的边 现在有两个问题 1.至少需要多少个放文件可以让整个图都有文件 2.可以进行一个操作:给一对点(u,v)连一条u->v的有向边, ...
- 【BZOJ 2809 dispatching】
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 4393 Solved: 2246[Submit][Status][Discuss] Descript ...
- CSS实现放大镜/狙击镜效果
图片放大,这是一个比较容易的效果了.当然,今天说的可不是简简单单的在一个框里放大,而是一个圆.就像放大镜或是狙击镜那样,只有圆圈里的放大,圈外的当然还是原来的图片.这是不是很不可思议? 当然不是.做过 ...
- 03 Java 修饰符
Java 修饰符主要分为两类: 访问修饰符 非访问修饰符 访问修饰符 public,对所有类可见 protected,对同一包内的类和子类可见 default,同一个包内的类可见 private,对当 ...