zoukankan      html  css  js  c++  java
  • Leetcode: 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.
    
    For 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

    Push all gates into queue first. Then for each gate update its neighbor cells who is empty and push them to the queue.

     Repeating above steps until there is nothing left in the queue.

     1 public class Solution {
     2     int[][] dirs = new int[][] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
     3     
     4     public void wallsAndGates(int[][] rooms) {
     5         if (rooms.length == 0 || rooms[0].length == 0) return;
     6         Queue<int[]> queue = new LinkedList<>();
     7         for (int i = 0; i < rooms.length; i++) {
     8             for (int j = 0; j < rooms[0].length; j++) {
     9                 if (rooms[i][j] == 0) queue.add(new int[]{i, j});
    10             }
    11         }
    12         int steps = 0;
    13         while (!queue.isEmpty()) {
    14             int size = queue.size();
    15             for (int i = 0; i < size; i ++) {
    16                 int[] cur = queue.poll();
    17                 for (int[] dir : dirs) {
    18                     int x = cur[0] + dir[0];
    19                     int y = cur[1] + dir[1];
    20                     if (x >= 0 && x < rooms.length && y >= 0 && y < rooms[0].length && rooms[x][y] == Integer.MAX_VALUE) {
    21                         rooms[x][y] = steps + 1;
    22                         queue.offer(new int[]{x, y});
    23                     }
    24                 }
    25             }
    26             steps ++;
    27         }
    28     }
    29 }

    Another way: similar

     1 public class Solution {
     2     public void wallsAndGates(int[][] rooms) {
     3         if (rooms.length == 0 || rooms[0].length == 0) return;
     4         Queue<int[]> queue = new LinkedList<>();
     5         for (int i = 0; i < rooms.length; i++) {
     6             for (int j = 0; j < rooms[0].length; j++) {
     7                 if (rooms[i][j] == 0) queue.add(new int[]{i, j});
     8             }
     9         }
    10         while (!queue.isEmpty()) {
    11             int[] top = queue.remove();
    12             int row = top[0], col = top[1];
    13             if (row > 0 && rooms[row - 1][col] == Integer.MAX_VALUE) {
    14                 rooms[row - 1][col] = rooms[row][col] + 1;
    15                 queue.add(new int[]{row - 1, col});
    16             }
    17             if (row < rooms.length - 1 && rooms[row + 1][col] == Integer.MAX_VALUE) {
    18                 rooms[row + 1][col] = rooms[row][col] + 1;
    19                 queue.add(new int[]{row + 1, col});
    20             }
    21             if (col > 0 && rooms[row][col - 1] == Integer.MAX_VALUE) {
    22                 rooms[row][col - 1] = rooms[row][col] + 1;
    23                 queue.add(new int[]{row, col - 1});
    24             }
    25             if (col < rooms[0].length - 1 && rooms[row][col + 1] == Integer.MAX_VALUE) {
    26                 rooms[row][col + 1] = rooms[row][col] + 1;
    27                 queue.add(new int[]{row, col + 1});
    28             }
    29         }
    30     }
    31 }
  • 相关阅读:
    Exceptionless in Docker on Linux 搭建及部署疑难杂症
    Golang 基础语法介绍及对比(二)
    Go Windows 环境安装及配置(一)
    ElasticSearch搜索解析
    ElasticSearch文档及分布式文档存储
    ElasticSearch利用IK实现全文搜索
    ElasticSearch简要总览
    轻松驾驭统计学——数据分析必备技能
    【面试】数据分析常见问题
    【Echarts】
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5077950.html
Copyright © 2011-2022 走看看