LeetCode Shortest Distance from All Buildings

原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/

题目:

You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where:

  • Each 0 marks an empty land which you can pass by freely.
  • Each 1 marks a building which you cannot pass through.
  • Each 2 marks an obstacle which you cannot pass through.

For example, given three buildings at (0,0)(0,4)(2,2), and an obstacle at (0,2):

1 - 0 - 2 - 0 - 1
|   |   |   |   |
0 - 0 - 0 - 0 - 0
|   |   |   |   |
0 - 0 - 1 - 0 - 0

The point (1,2) is an ideal empty land to build a house, as the total travel distance of 3+3+1=7 is minimal. So return 7.

题解:

从每一座building开始做BFS, 更新每个空地达到building的距离总和 以及 每个空地能到达building的个数.

第二次扫描grid, 若是空地并且它能到达的building数目是总共的building数目,就更新min距离.

Time Complexity: O(m^2 * n^2), 每次BFS用O(mn), 一共做了m*n次BFS. Space: O(m*n)

AC Java:

 1 public class Solution {
 2     public int shortestDistance(int[][] grid) {
 3         if(grid == null || grid.length == 0 || grid[0].length == 0){
 4             return -1;
 5         }
 6         int m = grid.length; 
 7         int n = grid[0].length;
 8         
 9         //记录到各个building距离和
10         int [][] dist = new int[m][n];
11         
12         //记录每个点能够到达building的个数
13         int [][] reachedNum = new int[m][n];
14         
15         //总共的building数目
16         int buildingNum = 0;
17         
18         for(int i = 0; i<m; i++){
19             for(int j = 0; j<n; j++){
20                 if(grid[i][j] == 1){
21                     //遇到building, 从这个building开始做bfs
22                     buildingNum++;
23                     bfs(grid, i, j, dist, reachedNum);
24                 }
25             }
26         }
27         
28         int min = Integer.MAX_VALUE;
29         for(int i = 0; i<m; i++){
30             for(int j = 0; j<n; j++){
31                 if(grid[i][j] == 0 && buildingNum == reachedNum[i][j]){ 
32                     //在空地上,并且这块空地能reach到所有building
33                     min = Math.min(min, dist[i][j]);
34                 }
35             }
36         }
37         
38         return min == Integer.MAX_VALUE ? -1 : min;
39     }
40     
41     private void bfs(int [][] grid, int i, int j, int [][] dist, int [][] reachedNum){
42         int [][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0,1}};
43         int m = grid.length;
44         int n = grid[0].length;
45         
46         boolean [][] visited = new boolean[m][n];
47         LinkedList<int []> que = new LinkedList<int []>();
48         
49         que.add(new int[]{i,j});
50         int level = 1;
51         int curCount = 1;
52         int nextCount = 0;
53         
54         while(!que.isEmpty()){
55             int [] cur = que.poll();
56             curCount--;
57             
58             for(int [] dir : dirs){
59                 int x = cur[0] + dir[0];
60                 int y = cur[1] + dir[1];
61                 if(x < 0 || x>= m || y<0 || y>=n || visited[x][y] || grid[x][y] != 0){
62                     continue;
63                 }
64                 visited[x][y] = true;
65                 que.add(new int[]{x,y});
66                 nextCount++;
67                 reachedNum[x][y]++;
68                 dist[x][y] += level;
69             }
70             
71             if(curCount == 0){
72                 curCount = nextCount;
73                 nextCount = 0;
74                 level++;
75             }
76         }
77     }
78 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/5318029.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值