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 = 2147483647
to representINF
as 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 ...
随机推荐
- 4.实现简单的shell sed替换功能
# -*- coding:utf-8 -*- # Author: JACK ZHAO # 程序1: 实现简单的shell sed替换功能 import sys #判断参数个数 if len(sys.a ...
- 聊聊、Java SPI
SPI,Service Provider Interface,服务提供者接口. Animal 接口 package com.rockcode.www.spi; public interface Ani ...
- 转: jsp之c标签
http://www.gbsou.com/2009/10/12/1028.htmljsp标签之c标签 核心标签库 它是JSTL中的核心库,为日常任务提供通用支持,如显示和设置变量.重复使用一组项目.测 ...
- hadoop-hdfs(三)
HDFS概念 1 数据块* HDFS的一个数据块默认是64M,与元数据分开管理. 优点: 数据块的大小设计的较大,所以寻址占传输的时间比例较小,只需要计算传输速度即可. 便于简化管理,利于计算剩余空间 ...
- JDK从1.8升级到9.0.1后sun.misc.BASE64Decoder和sun.misc.BASE64Encoder不可用
目录 描述 原因分析 处理办法 参考 描述 最近研究把项目的JDK升级从1.8升级到9.0.1,在eclipse上配置好JDK为9后,发现项目有错,查看发现sun.misc.BASE64Decoder ...
- InfluxDB数据备份和恢复方法,支持本地和远程备份
本文属于<InfluxDB系列教程>文章系列,该系列共包括以下 17 部分: InfluxDB学习之InfluxDB的基本概念 InfluxDB学习之InfluxDB的基本操作 Influ ...
- Maven入门指南(一)—— Maven下载与安装
Maven下载与安装 1.下载1)Maven的系统要求: Maven对内存和操作系统没有要求 Maven安装本身仅需大约10MB,本地仓库视使用情况有所不同 Maven3.3及以上版本需要JDK1.7 ...
- C# 代码片段
StringBuilder拼接小技巧 Stopwatch watch = new Stopwatch(); watch.Start(); var sb = new StringBuilder(); ; ...
- JS与jQuery中html-与-text方法的区别
所有的实例均用下面的html <div id="id0"> <div id="id1"> 直接text <p> <sp ...
- [leetcode]Convert Sorted Array to Binary Search Tre
排好序的... 中间是root , root左边是left,root右边是right 递归建树. /** * Definition for binary tree * struct TreeNode ...