My code:
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
private int row = 0;
private int col = 0;
private int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public int shortestDistance(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return -1;
}
this.row = grid.length;
this.col = grid[0].length;
int[][] distance = new int[row][col];
int[][] reach = new int[row][col];
int totalBuilding = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
totalBuilding++;
boolean[][] mark = new boolean[row][col];
Queue<int[]> q = new LinkedList<int[]>();
q.offer(new int[]{i, j});
mark[i][j] = true;
int level = 1;
while (!q.isEmpty()) {
int size = q.size();
for (int m = 0; m < size; m++) {
int[] loc = q.poll();
for (int k = 0; k < 4; k++) {
int next_x = loc[0] + dir[k][0];
int next_y = loc[1] + dir[k][1];
if (check(next_x, next_y) && !mark[next_x][next_y] && grid[next_x][next_y] == 0) {
mark[next_x][next_y] = true;
distance[next_x][next_y] += level;
reach[next_x][next_y]++;
q.offer(new int[]{next_x, next_y});
}
}
}
level++;
}
}
}
}
int ret = Integer.MAX_VALUE;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == 0 && reach[i][j] == totalBuilding && distance[i][j] < ret) {
ret = distance[i][j];
}
}
}
return ret == Integer.MAX_VALUE ? -1 : ret;
}
private boolean check(int i, int j) {
if (i < 0 || i >= row || j < 0 || j >= col) {
return false;
}
else {
return true;
}
}
}
reference:
https://discuss.leetcode.com/topic/31925/java-solution-with-explanation-and-time-complexity-analysis
这道题目让我想起了 multi-end BFS
就是从多个building同时出发,一起遍历。
但是问题在于,如何标志,这个 empty area 被多个Building访问过后的状态?
我没仔细想,直接看了答案。
目前的这个解法,感觉并不是最优的,时间复杂度达到了
O(m * n * m * n)
他解决我说的问题的方法是,
对building 一个个进行BFS,同时维护两个数组,一个累加距离,一个累加到这个点的building 个数。
最后再遍历这个距离数组,如果到这个点的building 个数 = 总building个数,那么这个点可以作为一个最短点,然后我们判断下他的总距离是否最小,如果最小,就更新最小值。
同时,记住, BFS的时候,我们需要一个变量level,这是必须的,用来记录每次距离应该加多少。
时间不知不觉到了10月份了。。好久没刷题了。
加油。未来,就在这最后一个月!
Optimization:
My code:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Solution {
private class Tuple {
int x;
int y;
int distance;
Tuple(int x, int y, int distance) {
this.x = x;
this.y = y;
this.distance = distance;
}
}
private int row = 0;
private int col = 0;
private int[][] dir = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public int shortestDistance(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return -1;
}
row = grid.length;
col = grid[0].length;
int[][] dist = new int[row][col];
List<Tuple> buildings = new ArrayList<Tuple>();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == 1) {
buildings.add(new Tuple(i, j, 0));
}
grid[i][j] = -grid[i][j];
}
}
for (int i = 0; i < buildings.size(); i++) {
bfs(buildings.get(i), i, grid, dist);
}
int ret = Integer.MAX_VALUE;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == buildings.size() && dist[i][j] < ret) {
ret = dist[i][j];
}
}
}
return ret == Integer.MAX_VALUE ? -1 : ret;
}
private void bfs(Tuple root, int k, int[][] grid, int[][] dist) {
Queue<Tuple> q = new LinkedList<Tuple>();
q.offer(root);
while (!q.isEmpty()) {
Tuple t = q.poll();
dist[t.x][t.y] += t.distance;
for (int i = 0; i < 4; i++) {
int next_x = t.x + dir[i][0];
int next_y = t.y + dir[i][1];
if (check(next_x, next_y) && grid[next_x][next_y] == k) {
q.offer(new Tuple(next_x, next_y, t.distance + 1));
grid[next_x][next_y] = k + 1;
}
}
}
}
private boolean check(int i, int j) {
if (i < 0 || i >= row || j < 0 || j >= col) {
return false;
}
return true;
}
public static void main(String[] args) {
Solution test = new Solution();
int[][] input = new int[][]{{1, 0, 2, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 1, 0, 0}};
int ret = test.shortestDistance(input);
System.out.println(ret);
}
}
reference:
https://discuss.leetcode.com/topic/32391/share-a-java-implement
做完这题,总感觉之前的解法,有太多重复运算。
于是看了更好的解法。
之前的重复在于,如果一个index,他确定不能被一个大楼走到,那么以后对于其他大楼,我们都不需要对他做BFS了,也就是剪枝。
然后现在这个做法,可以很好地剪枝。
Anyway, Good luck, Richardo! — 10/07/2016