zoukankan      html  css  js  c++  java
  • 165-286. 墙与门

    你被给定一个 m × n 的二维网格,网格中有以下三种可能的初始化值:
    
    -1 表示墙或是障碍物
    0 表示一扇门
    INF 无限表示一个空的房间。然后,我们用 231 - 1 = 2147483647 代表 INF。你可以认为通往门的距离总是小于 2147483647 的。
    你要给每个空房间位上填上该房间到 最近 门的距离,如果无法到达门,则填 INF 即可。
    
    示例:
    
    给定二维网格:
    
    INF  -1  0  INF
    INF INF INF  -1
    INF  -1 INF  -1
      0  -1 INF INF
    运行完你的函数后,该网格应该变成:
    
      3  -1   0   1
      2   2   1  -1
      1  -1   2  -1
      0  -1   3   4
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/walls-and-gates
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
    
    package com.atguigu.mycode;
    
    import java.util.*;
    
    /**
     * @author nanhai
     * @create 2021-02-02 14:42
     */
    public class Solution {
    
        public static void wallsAndGates(int[][] rooms){
            for(int i=0; i< rooms.length; i++){
                for(int j=0; j<rooms[i].length; j++){
                    if(rooms[i][j]==0){
                        dsf(rooms, i, j, 0);
                    }
                }
            }
        }
    
        public static void dsf(int[][] rooms, int i, int j, int step){
            if(i< 0 || i>= rooms.length || j < 0 || j >=rooms[i].length || rooms[i][j] < step) return;
            rooms[i][j] = step;
            dsf(rooms,i+1,j, step + 1);
            dsf(rooms,i,j+1, step + 1);
            dsf(rooms,i-1,j, step + 1);
            dsf(rooms,i,j-1, step + 1);
        }
    
        public static void main(String[] args){
            Integer inf = Integer.MAX_VALUE;
            int[][] rooms = new int[4][4];
            rooms[0] = new int[]{inf, -1, 0, inf};
            rooms[1] = new int[]{inf, inf, inf, -1};
            rooms[2] = new int[]{inf, -1, inf, 0};
            rooms[3] = new int[]{0, -1, inf, inf};
    //        wallsAndGates(rooms);
    //        System.out.println(rooms);
            Solution2 solution2 = new Solution2();
            solution2.wallsAndGates(rooms);
            solution2.bsf(rooms);
            System.out.println(rooms);
        }
    
    }
    
    
    class Solution2{
        private static Queue<int[]> queue = new LinkedList<>();
        public void wallsAndGates(int[][] rooms){
            for(int i=0; i< rooms.length; i++){
                for(int j=0; j<rooms[i].length; j++){
                    if(rooms[i][j]==0){
                        queue.add(new int[]{i, j});
                    }
                }
            }
        }
        public void bsf(int[][] rooms){
            int[] x_ = new int[]{1, -1, 0, 0};
            int[] y_ = new int[]{0, 0, 1, -1};
            while(!queue.isEmpty()){
                int[] cur = queue.poll();
                int x0 = cur[0];
                int y0 = cur[1];
                for(int i=0; i<4; i++){
                    int x = x0 + x_[i];
                    int y = y0 + y_[i];
                    if (0 <= x && x <rooms.length && 0<=y && y < rooms[i].length && rooms[x][y] == Integer.MAX_VALUE){
                        rooms[x][y] = rooms[x0][y0] + 1;
                        queue.add(new int[]{x, y});
                    }
                }
            }
        }
    
    }
    
  • 相关阅读:
    (5.1.4)引擎管理——SSMS管理使用小技巧
    (5.1.3)引擎管理——恢复系统数据库
    Kafka 基本原理
    读懂基础机器学习算法
    数据挖掘常用工具
    Oracle 增加修改删除字段
    .net中使用ODP.net访问Oracle数据库(无客户端部署方法)
    数据挖掘系列(1)关联规则挖掘基本概念与Aprior算法
    ORACLE存储过程学习
    js版扫雷(可直接运行试玩)
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/14362752.html
Copyright © 2011-2022 走看看