原题链接在这里: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 Walls and Gates的更多相关文章

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

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

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

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

  5. LeetCode 286. Walls and Gates

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

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

  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. AIM Tech Round 3 (Div. 2)

    #include <iostream> using namespace std; ]; int main() { int n, b, d; cin >> n >> ...

  2. hiveserver2

    hiveserver2 默认绑定了ip:localhost 和 port:10000 !connect jdbc:hive2://localhost:10000  org.apache.hive.jd ...

  3. 《你必须知道的.NET》书中对OCP(开放封闭)原则的阐述

    开放封闭原则(OCP,Open Closed Principle)是面向对象原则的核心.由于软件设计本身所追求的墓边就是封装变化,降低耦合,而开放封闭原则就是对这一目标的直接体现.(你必须知道的.NE ...

  4. jQuery 循环问题

    $("#add2sub").click(function(){ var $sxarr=$(".add_shuxing_ul > .sx_add_bg"); ...

  5. HTTP Methods: GET v.s POST

    HTTP works as a request-response protocol between a client and server. A web browser may be the clie ...

  6. [LintCode] Wiggle Sort II 扭动排序之二

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  7. Odoo 仓库扫码打包方案

        Odoo仓库扫码的设计,前提是操作人在PC上先做好分拣单,然后根据打印出来的分拣单去仓库进行扫码打包,默认的情况下,分拣在被确认的时候会自动保留库位中已经存在的库存(已经分配批次\序列号),而 ...

  8. 行业集中度(Concentration Ratio)

    行业集中度是决定市场结构最基本.最重要的因素,集中体现了市场的竞争和垄断程度,经常使用的集中度计量指标有:行业集中率(CRn指数).赫尔芬达尔—赫希曼指数(Herfindahl-HirschmanIn ...

  9. iframe自适应高度js

    <iframe src="http://www.fufuok.com/" id="iframepage" name="iframepage&qu ...

  10. jQuery设计思想之取值和赋值

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...