码迷,mamicode.com
首页 > 其他好文 > 详细

[LintCode] 574 Build Post Office II

时间:2017-05-15 09:48:26      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:from   within   offer   public   注意   amp   list   class   queue   

Problem

Given a 2D grid, each cell is either a wall 2, an house 1 or empty 0 (the number zero, one, two), find the place to build a post office, the distance that post office to all the house sum is smallest. Return the smallest distance. Return -1 if it is not possible.

Notice

You cannot pass through wall and house, but can pass through empty.You only build post office on an empty.

Example

Given a grid:

0 1 0 0 0
1 0 0 2 1
0 1 0 0 0

return 8, You can build at (1,1). (Placing a post office at (1,1), the distance that post office to all the house sum is smallest.)

Challenge

Solve this problem within O(n^3) time.

5/14/2017

算法班,抄答案的,未经验证

注意第65行,是否需要判断当前是不是wall?

 1 class Coordinate {
 2     int x, y;
 3     public Coordinate(int x, int y) {
 4         this.x = x;
 5         this.y = y;
 6     }
 7 }
 8 
 9 public class Solution {
10 
11     public int EMPTY = 0;
12     public int HOUSE = 1;
13     public int WALL = 2;
14 
15     public int[] xDirection = {0, 0, 1, -1};
16     public int[] yDirection = {1, -1, 0, 0};
17 
18     /**
19      * @param grid a 2D grid
20      * @return an integer
21      */
22     public int shortestDistance(int[][] grid) {
23         if (grid == null || grid.length == 0 || grid[0].length == 0) {
24             return -1;
25         }
26 
27         int[][] distanceSum = new int[grid.length][grid[0].length]; // total distance from all houses to current empty spot
28         int[][] visitedTimes = new int[grid.length][grid[0].length]; // total visited times from all houses to current empty spot
29 
30         List<Coordinate> houses = getCoordinates(grid, HOUSE);
31 
32         for (Coordinate house: houses) {
33             bfs(house, grid, distanceSum, visitedTimes);
34         }
35 
36         List<Coordinate> empties = getCoordinates(grid, EMPTY);
37         int shortest = Integer.MAX_VALUE;
38         for (Coordinate empty: empties) {
39             if (visitedTimes[empty.x][empty.y] != houses.size()) {
40                 continue;
41             }
42             shortest = Math.min(shortest, distanceSum[empty.x][empty.y]);
43         }
44         if (shortest == Integer.MAX_VALUE) return -1;
45         return shortest;
46     }
47 
48     private void bfs(Coordinate house, int[][] grid, int[][] distanceSum, int[][] visitedTimes) {
49         Queue<Coordinate> queue = new LinkedList<Coordinate>();
50         boolean[][] hash = new boolean[grid.length][grid[0].length];
51 
52         queue.offer(house);
53 
54         int step = 0;
55         while (!queue.isEmpty()) {
56             step++;
57             int size = queue.size();
58             for (int i = 0; i < size; i++) {
59                 Coordinate c = queue.poll();
60                 for (int j = 0; j < xDirection.length; j++) {
61                     Coordinate a = new Coordinate(c.x + xDirection[j], c.y + yDirection[j]);
62                     if (!inBound(a)) continue;
63                     if (hash[a.x][a.y]) continue;
64                     // do we need this check?
65                     if (grid[a.x][a.y] == WALL) continue;
66                     queue.offer(a);
67                     hash[a.x][a.y] = true;
68                     distanceSum[a.x][a.y] += step;
69                     visitedTimes[a.x][a.y]++;
70                 }
71             }
72         }
73     }
74 
75     private List<Coordinate> getCoordinates(int[][] grid, int type) {
76         List<Coordinate> ret = new ArrayList<Coordinate>();
77 
78         for (int i = 0; i < grid.length; i++) {
79             for (int j = 0; j < grid[0].length; j++) {
80                 if (grid[i][j] == type) {
81                     ret.add(new Coordinate(i, j));
82                 }
83             }
84         }
85         return ret;
86     }
87     private boolean inBound(Coordinate coor) {
88         if (coor.x < 0 || coor.x >= n) {
89             return false;
90         }
91         if (coor.y < 0 || coor.y >= m) {
92             return false;
93         }
94         return grid[coor.x][coor.y] == EMPTY;
95     }
96 }

 

[LintCode] 574 Build Post Office II

标签:from   within   offer   public   注意   amp   list   class   queue   

原文地址:http://www.cnblogs.com/panini/p/6854694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!