题目地址:https://leetcode-cn.com/problems/walls-and-gates/

题目描述

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 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.

Example:

Given the 2D grid:

INF  -1  0  INF
INF INF INF -1
INF -1 INF -1
0 -1 INF INF
After running your function, the 2D grid should be: 3 -1 0 1
2 2 1 -1
1 -1 2 -1
0 -1 3 4

题目大意

你被给定一个 m × n 的二维网格,网格中有以下三种可能的初始化值:

  1. -1 表示墙或是障碍物
  2. 0 表示一扇门
  3. INF 无限表示一个空的房间。然后,我们用 231 - 1 = 2147483647 代表 INF。你可以认为通往门的距离总是小于 2147483647 的。

你要给每个空房间位上填上该房间到 最近 门的距离,如果无法到达门,则填 INF 即可。

解题方法

BFS

这个题可以使用BFS和DFS去解决,我选用的BFS。

  1. 找出所有为0的位置,放入队列中
  2. 从为0的位置开始向四个方向遍历,直接修改迷宫的可以走的位置的数字是距离门最近的距离
  3. 把新的可以走的位置放入队列中,继续搜索

C++代码如下:

class Solution {
public:
void wallsAndGates(vector<vector<int>>& rooms) {
if (rooms.empty() || rooms[0].empty()) return;
M = rooms.size();
N = rooms[0].size();
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (rooms[i][j] == 0) {
bfs(rooms, i, j);
}
}
}
}
void bfs(vector<vector<int>>& rooms, int x, int y) {
if (rooms[x][y] != 0) return;
queue<vector<int>> que;
que.push({x, y});
while (!que.empty()) {
vector<int> cur = que.front(); que.pop();
for (auto& dir : dirs) {
int newx = cur[0] + dir[0];
int newy = cur[1] + dir[1];
if (newx < 0 || newx >= M || newy < 0 || newy >= N
|| rooms[newx][newy] <= rooms[cur[0]][cur[1]])
continue;
rooms[newx][newy] = rooms[cur[0]][cur[1]] + 1;
que.push({newx, newy});
}
}
}
private:
int M, N;
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
};

日期

2019 年 9 月 23 日 —— 昨夜睡的早,错过了北京的烟火

【LeetCode】286. Walls and Gates 解题报告 (C++)的更多相关文章

  1. [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 ...

  2. LeetCode 286. Walls and Gates

    原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...

  3. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  4. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  5. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  8. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  9. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

随机推荐

  1. Go 命令类型和未命名类型

    Go 命令类型和未命名类型 例子 package main import "fmt" // 使用type声明的是命令类型 // type new_type old_type typ ...

  2. Mysql-多个left join 计算逻辑

    单个left join: (1)一对一:结果表的行数=左表行数 (2)一对多:结果表的行数>左表行数 多个left join: (0)多个left join由上到下,依次生成查询表,原理同单个l ...

  3. Demo02一千以内的水仙花数

    package 习题集2;//1000以内的水仙花数public class Demo02 { public static void main(String[] args) { int i = 100 ...

  4. Office2020-2021 离线安装教程

    首先:先安装两个 net .再安装 office 如下: DownFile:https://pan.baidu.com/s/19iykxwofXK36wWY5w4GVFg  codenum:6666

  5. 学习java的第二十一天

    一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...

  6. ping (网络诊断工具)

    Ping是Windows.Unix和Lnix系统下的一个命令,ping也属于一个通信协议,是TCP/IP协议的一部分,利用Ping命令可以检查网络是否连通,可以很好地帮助我们分析和判定网络故障.应用格 ...

  7. Copy constructor vs assignment operator in C++

    Difficulty Level: Rookie Consider the following C++ program. 1 #include<iostream> 2 #include&l ...

  8. archive后upload to app store时遇到app id不可用的问题

    问题如下图 出现此问题的原因有两种: 1.此app id在AppStore中已经存在,也就是说你使用别人注册的app ID ,  如果是这样,你只能更换app ID 2.此app ID是自己的,突然之 ...

  9. CentOS6设置Django开发环境

    今天在我的Centos6.5机器上安装 Django 开发环境,在安装完使用 "django-admin.py startproject myapp" 创建应用的时候报了下面的错误 ...

  10. 通过js进行页面跳转的几种方式

    1.<a>标签 <a href="www.baidu.com" title="百度">百度</a> <a href= ...