Build Post Office II

本文介绍了一种算法,用于在一个由墙壁、房屋和空地组成的二维网格中找到建造邮局的最佳位置,使得从该邮局到所有房屋的总距离最短。通过广度优先搜索遍历所有可能的位置,并计算最小总距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a 2D grid, each cell is either a wall 2, an house 1 or empty 0 (the number zero, one, two), find a place to build a post office so that the sum of the distance from the post office to all the houses is smallest.

Return the smallest sum of distance. Return -1 if it is not possible.

class Coordinate {
    int x;
    int y;
    public Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
public class Solution {
    /**
     * @param grid a 2D grid
     * @return an integer
     */
    public int EMPTY = 0;
    public int HOUSE = 1;
    public int WALL = 2;
    public int n;
    public int m;
    public int[][] grid;
    //四个方向
    public int[] directionX = {0, 0, 1, -1};
    public int[] directionY = {1, -1, 0, 0};
    //初始化n, m, grid
    private void setGrid(int[][] grid) {
        this.n = grid.length;
        this.m = grid[0].length;
        this.grid = grid;
    }
    //获取1,0的坐标序列
    private ArrayList<Coordinate> getCoordinates(int type) {
        ArrayList<Coordinate> coordinates = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == type) {
                    coordinates.add(new Coordinate(i, j));
                }
            }
        }
        return coordinates;
    }
    //判断坐标是否valid
    private boolean inBound(int x, int y, int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        return x >= 0 && x < n && y >= 0 && y < m && grid[x][y] == 0;
    }
    //从房子出发,累加房子到每个空白的距离
    public int shortestDistance(int[][] grid) {
        if (grid == null || grid.length == 0 || grid[0].length == 0) {
            return -1;
        }
        setGrid(grid);
        ArrayList<Coordinate> house = getCoordinates(HOUSE);
        //累加数组
        int[][] distanceSum = new int[n][m];
        //标记房子来过的记录,如果最后记录!=房子的数目,则说明有些空白不可达
        int[][] visited = new int[n][m];
        for (Coordinate h : house) {
            bfsHelper(h, distanceSum, visited);
        }
        int shortest = Integer.MAX_VALUE;
        ArrayList<Coordinate> empty = getCoordinates(EMPTY);
        for (Coordinate e : empty) {
            if (visited[e.x][e.y] != house.size()) {
                continue;
            }
            shortest = Math.min(shortest, distanceSum[e.x][e.y]);
        }
        if (shortest == Integer.MAX_VALUE) {
            return -1;
        }
        return shortest;
    }
    private void bfsHelper(Coordinate target, int[][] distanceSum, int[][] visited) {
        Queue<Coordinate> queue = new LinkedList<>();
        queue.offer(new Coordinate(target.x, target.y));
        boolean[][] hash = new boolean[n][m];
        hash[target.x][target.y] = true;
        int step = 0;
        while (!queue.isEmpty()) {
            step++;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Coordinate coor = queue.poll();
                for (int j = 0; j < 4; j++) {
                    Coordinate adj = new Coordinate(
                        coor.x + directionX[j],
                        coor.y + directionY[j]
                    );
                    if (!inBound(adj.x, adj.y, grid)) {
                        continue;
                    }
                    if (hash[adj.x][adj.y]) {
                        continue;
                    }
                    queue.offer(adj);
                    distanceSum[adj.x][adj.y] += step;
                    visited[adj.x][adj.y]++;
                    hash[adj.x][adj.y] = true;
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值