573.Build Post Office II
Last updated
Last updated
//这里加了一个dis参数
class Node{
public int x,y,dis;
public Node(int x,int y,int dis){
this.x=x;
this.y=y;
this.dis=dis;
}
}
public class Solution_573 {
/**
* @param grid a 2D grid
* @return an integer
*/
public int m,n;
public int[] deltaX={0,0,1,-1};
public int[] deltaY={1,-1,0,0};
public int shortestDistance(int[][] grid) {
if(grid==null || grid.length==0 || grid[0].length==0){
return -1;
}
m=grid.length;
n=grid[0].length;
//Find all the house
ArrayList<Node> house=new ArrayList<Node>();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==1){
Node node=new Node(i,j,0);
house.add(node);
}
}
}
int housecount=house.size();
//distance[i][j][k]为k房子到grid[i][j]上的点的距离
int[][][] distance=new int[housecount][m][n];
for(int i=0;i<housecount;i++){
for(int j=0;j<m;j++){
Arrays.fill(distance[i][j], Integer.MAX_VALUE);
}
}
for(int i=0;i<housecount;i++){
findDistance(house.get(i),distance,i,grid);
}
int min=Integer.MAX_VALUE;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==0){
int sum=0;
for(int s=0;s<housecount;s++){
if(distance[s][i][j]==Integer.MAX_VALUE){
sum=Integer.MAX_VALUE;
break;
}
sum+=distance[s][i][j];
}
min=Math.min(min,sum);
}
}
}
if(min==Integer.MAX_VALUE){
return -1;
}
return min;
}
//Use bfs to fill the distance[][][]
public void findDistance(Node root,int[][][] distance,int k,int[][] grid){
Queue<Node> queue=new LinkedList<Node>();
queue.offer(root);
boolean[][] visited=new boolean[m][n];
visited[root.x][root.y]=true;
while(!queue.isEmpty()){
Node current=queue.poll();
for(int direction=0;direction<4;direction++){
int neighborX=current.x+deltaX[direction];
int neighborY=current.y+deltaY[direction];
//这里括号里的顺序很重要
if(neighborX>=0 && neighborX<m && neighborY>=0 && neighborY<n
&& grid[neighborX][neighborY]==0 && !visited[neighborX][neighborY]){
distance[k][neighborX][neighborY]=current.dis+1;
Node neighbor=new Node(neighborX,neighborY,current.dis+1);
queue.offer(neighbor);
visited[neighborX][neighborY]=true;
}
}
}
}
}