原题链接在这里:https://leetcode.com/problems/walls-and-gates/

题目:

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

  1. INF -1 0 INF
  2. INF INF INF -1
  3. INF -1 INF -1
  4. 0 -1 INF INF

After running your function, the 2D grid should be:

  1. 3 -1 0 1
  2. 2 2 1 -1
  3. 1 -1 2 -1
  4. 0 -1 3 4

题解:

BFS, 先把所有gate加到que中。对于每一个从que中poll出来的gate,看四个方向是否有room, 若有,把room的值改正gate + 1, 在放回到que中.

Time Complexity: O(m*n). m = rooms.length, n = rooms[0].length. 每个点没有扫描超过两遍. Space: O(m*n).

AC Java:

  1. class Solution {
  2. public void wallsAndGates(int[][] rooms) {
  3. if(rooms == null || rooms.length == 0 || rooms[0].length == 0){
  4. return;
  5. }
  6.  
  7. int m = rooms.length;
  8. int n = rooms[0].length;
  9. LinkedList<int []> que = new LinkedList<>();
  10. int level = 1;
  11. for(int i = 0; i < m; i++){
  12. for(int j = 0; j < n; j++){
  13. if(rooms[i][j] == 0){
  14. que.add(new int[]{i, j});
  15. }
  16. }
  17. }
  18.  
  19. int [][] dirs = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  20.  
  21. while(!que.isEmpty()){
  22. int size = que.size();
  23. while(size-- > 0){
  24. int [] cur = que.poll();
  25. for(int [] dir : dirs){
  26. int x = cur[0] + dir[0];
  27. int y = cur[1] + dir[1];
  28. if(x < 0 || x >= m || y < 0 || y >= n || rooms[x][y] != Integer.MAX_VALUE){
  29. continue;
  30. }
  31.  
  32. que.add(new int[]{x, y});
  33. rooms[x][y] = level;
  34. }
  35. }
  36.  
  37. level++;
  38. }
  39. }
  40. }

跟上Robot Room CleanerRotting OrangesShortest Distance from All Buildings.

LeetCode 286. Walls and Gates的更多相关文章

  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 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

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

  4. [LeetCode] 286. Walls and Gates_Medium tag: BFS

    You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...

  5. 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的值. 最 ...

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

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

  8. LeetCode Walls and Gates

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

  9. Walls and Gates -- LeetCode

    You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...

随机推荐

  1. 案例:使用logstash收集游戏服务器日志,输出到kafka消息队列中,然后存入ES

    gamelogs2kafka.conf input { file { codec => plain { charset => "UTF-8" } path => ...

  2. springboot yml配置文件注入值

    1.编写javabean: package com.example.springboot.bean; import org.springframework.boot.context.propertie ...

  3. 在Eclipse中使用Beyond Compare做为比较工具

    1.下载org.eclipse.externaltools-Update-0.8.9.v201003051612.zip插件包 接下来,要下载Beyond Compare的插件,http://beyo ...

  4. using 语句(C# 参考)(转载)

    using 语句 提供可确保正确使用 IDisposable对象的方便语法. 示例 下面的示例演示如何使用 using 语句. using (var font1 = new Font("Ar ...

  5. ASP.NET Core中如何显示[PII is hidden]的隐藏信息

    有时候我们在ASP.NET Core项目运行时,发生在后台程序中的错误会将关键信息隐藏为[PII is hidden]这种占位符,如下所示: 而知道这些关键信息,有时候对我们调试程序是非常重要的.所以 ...

  6. C#读写调整设置UVC摄像头画面-亮度

    有时,我们需要在C#代码中对摄像头的亮度进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...

  7. CKEditor 4 上传图片

    参考资料:Basic Configuration 直接Ctrl+v(粘贴图片)报错信息:上传文件时发生网络错误(networkError:Network error occurred during f ...

  8. Python基础之面向对象编程

    面向对象编程 —— Object Oriented Programming 简写 OOP 01. 面向对象基本概念 我们之前学习的编程方式就是 面向过程 的 面向过程 和 面向对象,是两种不同的 编程 ...

  9. Spring Security 解析(三) —— 个性化认证 以及 RememberMe 实现

    Spring Security 解析(三) -- 个性化认证 以及 RememberMe 实现   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...

  10. 【转载】 Windows系统电脑通过设备管理器查看电脑配置信息

    在采购电脑或者使用电脑的过程中,有时候我们需要查看到电脑的所有设备硬件信息,此时就可以通过Windows系统自带的设备管理器界面来查看该电脑所有的设备配置信息,包括CPU型号频率.内存.硬盘型号以及容 ...