@@ -28,58 +28,64 @@ The point (1,2) is an ideal empty land to build a house, as the total travel dis
28
28
*/
29
29
30
30
public class _317 {
31
+ public static class Solution1 {
32
+ public int shortestDistance (int [][] grid ) {
33
+ int m = grid .length ;
34
+ if (m == 0 ) {
35
+ return -1 ;
36
+ }
37
+ int n = grid [0 ].length ;
38
+ int [][] reach = new int [m ][n ];
39
+ int [][] distance = new int [m ][n ];
40
+ int [] shift = new int [] {0 , 1 , 0 , -1 ,
41
+ 0 };//how these five elements is ordered is important since it denotes the neighbor of the current node
42
+ int numBuilding = 0 ;
31
43
32
- public int shortestDistance (int [][] grid ) {
33
- int m = grid .length ;
34
- if (m == 0 ) {
35
- return -1 ;
36
- }
37
- int n = grid [0 ].length ;
38
- int [][] reach = new int [m ][n ];
39
- int [][] distance = new int [m ][n ];
40
- int [] shift = new int []{0 , 1 , 0 , -1 , 0 };//how these five elements is ordered is important since it denotes the neighbor of the current node
41
- int numBuilding = 0 ;
42
-
43
- for (int i = 0 ; i < m ; i ++) {
44
- for (int j = 0 ; j < n ; j ++) {
45
- if (grid [i ][j ] == 1 ) {
46
- numBuilding ++;
47
- int dist = 1 ;
48
- boolean [][] visited = new boolean [m ][n ];
44
+ for (int i = 0 ; i < m ; i ++) {
45
+ for (int j = 0 ; j < n ; j ++) {
46
+ if (grid [i ][j ] == 1 ) {
47
+ numBuilding ++;
48
+ int dist = 1 ;
49
+ boolean [][] visited = new boolean [m ][n ];
49
50
50
- Queue <int []> q = new LinkedList <int []>();
51
- q .offer (new int []{i , j });
52
- while (!q .isEmpty ()) {
53
- int size = q .size ();
54
- for (int l = 0 ; l < size ; l ++) {
55
- int [] current = q .poll ();
56
- for (int k = 0 ; k < 4 ; k ++) {
57
- int nextRow = current [0 ] + shift [k ];
58
- int nextCol = current [1 ] + shift [k + 1 ];
59
- if (nextRow >= 0 && nextRow < m && nextCol >= 0
60
- && nextCol < n && !visited [nextRow ][nextCol ] && grid [nextRow ][nextCol ] == 0 ) {
61
- distance [nextRow ][nextCol ] += dist ;
62
- visited [nextRow ][nextCol ] = true ;
63
- reach [nextRow ][nextCol ]++;
64
- q .offer (new int []{nextRow , nextCol });
51
+ Queue <int []> q = new LinkedList <int []>();
52
+ q .offer (new int [] {i , j });
53
+ while (!q .isEmpty ()) {
54
+ int size = q .size ();
55
+ for (int l = 0 ; l < size ; l ++) {
56
+ int [] current = q .poll ();
57
+ for (int k = 0 ; k < 4 ; k ++) {
58
+ int nextRow = current [0 ] + shift [k ];
59
+ int nextCol = current [1 ] + shift [k + 1 ];
60
+ if (nextRow >= 0
61
+ && nextRow < m
62
+ && nextCol >= 0
63
+ && nextCol < n
64
+ && !visited [nextRow ][nextCol ]
65
+ && grid [nextRow ][nextCol ] == 0 ) {
66
+ distance [nextRow ][nextCol ] += dist ;
67
+ visited [nextRow ][nextCol ] = true ;
68
+ reach [nextRow ][nextCol ]++;
69
+ q .offer (new int [] {nextRow , nextCol });
70
+ }
65
71
}
66
72
}
73
+ dist ++;
67
74
}
68
- dist ++;
69
75
}
70
76
}
71
77
}
72
- }
73
78
74
- int result = Integer .MAX_VALUE ;
75
- for (int i = 0 ; i < m ; i ++) {
76
- for (int j = 0 ; j < n ; j ++) {
77
- if (grid [i ][j ] == 0 && reach [i ][j ] == numBuilding && distance [i ][j ] < result ) {
78
- result = distance [i ][j ];
79
+ int result = Integer .MAX_VALUE ;
80
+ for (int i = 0 ; i < m ; i ++) {
81
+ for (int j = 0 ; j < n ; j ++) {
82
+ if (grid [i ][j ] == 0 && reach [i ][j ] == numBuilding && distance [i ][j ] < result ) {
83
+ result = distance [i ][j ];
84
+ }
79
85
}
80
86
}
87
+ return result == Integer .MAX_VALUE ? -1 : result ;
81
88
}
82
- return result == Integer .MAX_VALUE ? -1 : result ;
83
89
}
84
90
85
91
}
0 commit comments