6262 */
6363
6464public class TheMazeII505 {
65- private int [][] directions = new int [][]{{0 , 1 }, {0 , -1 }, {1 , 0 }, {-1 , 0 }};
65+ private int [][] DIRECTIONS = new int [][]{{0 , 1 }, {0 , -1 }, {1 , 0 }, {-1 , 0 }};
6666 public int shortestDistance (int [][] maze , int [] start , int [] destination ) {
6767 int M = maze .length ;
6868 int N = maze [0 ].length ;
@@ -82,7 +82,7 @@ public int shortestDistance(int[][] maze, int[] start, int[] destination) {
8282 min = Math .min (min , curr .dist );
8383 continue ;
8484 }
85- for (int [] d : directions ) {
85+ for (int [] d : DIRECTIONS ) {
8686 int x = curr .pos [0 ] + d [0 ];
8787 int y = curr .pos [1 ] + d [1 ];
8888 int more = 1 ;
@@ -130,10 +130,8 @@ public int shortestDistance2(int[][] maze, int[] start, int[] destination) {
130130 }
131131 int [] dx = new int [] {-1 , 0 , 1 , 0 };
132132 int [] dy = new int [] { 0 , 1 , 0 , -1 };
133-
134133 q .offer (start );
135134 dist [start [0 ]][start [1 ]] = 0 ;
136-
137135 while (!q .isEmpty ()) {
138136 int [] p = q .poll ();
139137 for (int i = 0 ; i < 4 ; i ++) {
@@ -258,6 +256,99 @@ public void dfs(int[][] maze, int[] start, int[][] distance) {
258256 }
259257
260258
259+ /**
260+ * https://leetcode.com/problems/the-maze-ii/solution/
261+ */
262+ public int shortestDistance5 (int [][] maze , int [] start , int [] destination ) {
263+ int M = maze .length ;
264+ int N = maze [0 ].length ;
265+ int [][] distances = new int [M ][N ];
266+ for (int [] row : distances ) Arrays .fill (row , Integer .MAX_VALUE );
267+ distances [start [0 ]][start [1 ]] = 0 ;
268+ boolean [][] visited = new boolean [M ][N ];
269+ roll (maze , distances , visited , M , N );
270+ int res = distances [destination [0 ]][destination [1 ]];
271+ return res == Integer .MAX_VALUE ? -1 : res ;
272+ }
273+
274+ private void roll (int [][] maze , int [][] distances , boolean [][] visited , int M , int N ) {
275+ while (true ) {
276+ int [] m = minDistance (distances , visited , M , N );
277+ if (m == null ) return ;
278+ visited [m [0 ]][m [1 ]] = true ;
279+ for (int [] dir : DIRECTIONS ) {
280+ int x = m [0 ] + dir [0 ];
281+ int y = m [1 ] + dir [1 ];
282+ int dis = 0 ;
283+ while (x >= 0 && y >= 0 && x < M && y < N && maze [x ][y ] == 0 ) {
284+ x += dir [0 ];
285+ y += dir [1 ];
286+ dis ++;
287+ }
288+ x -= dir [0 ];
289+ y -= dir [1 ];
290+ int d = distances [m [0 ]][m [1 ]] + dis ;
291+ if (d < distances [x ][y ]) {
292+ distances [x ][y ] = d ;
293+
294+ }
295+ }
296+ }
297+ }
261298
299+ private int [] minDistance (int [][] distances , boolean [][] visited , int M , int N ) {
300+ int [] res = null ;
301+ int min = Integer .MAX_VALUE ;
302+ for (int i =0 ; i <M ; i ++) {
303+ for (int j =0 ; j <N ; j ++) {
304+ if (!visited [i ][j ] && distances [i ][j ] < min ) {
305+ res = new int []{i , j };
306+ min = distances [i ][j ];
307+ }
308+ }
309+ }
310+ return res ;
311+ }
312+
313+
314+ /**
315+ * https://leetcode.com/problems/the-maze-ii/solution/
316+ */
317+ public int shortestDistance6 (int [][] maze , int [] start , int [] destination ) {
318+ int M = maze .length ;
319+ int N = maze [0 ].length ;
320+ int [][] distances = new int [M ][N ];
321+ for (int [] row : distances ) Arrays .fill (row , Integer .MAX_VALUE );
322+ distances [start [0 ]][start [1 ]] = 0 ;
323+ roll (maze , distances , start , M , N );
324+ int res = distances [destination [0 ]][destination [1 ]];
325+ return res == Integer .MAX_VALUE ? -1 : res ;
326+ }
327+
328+ private void roll (int [][] maze , int [][] distances , int [] start , int M , int N ) {
329+ PriorityQueue <int []> pq = new PriorityQueue <>((a , b ) -> a [2 ] - b [2 ]);
330+ pq .add (new int []{start [0 ], start [1 ], 0 });
331+ while (!pq .isEmpty ()) {
332+ int [] md = pq .poll ();
333+ if (distances [md [0 ]][md [1 ]] < md [2 ]) continue ;
334+ for (int [] dir : DIRECTIONS ) {
335+ int x = md [0 ] + dir [0 ];
336+ int y = md [1 ] + dir [1 ];
337+ int dis = 0 ;
338+ while (x >= 0 && y >= 0 && x < M && y < N && maze [x ][y ] == 0 ) {
339+ x += dir [0 ];
340+ y += dir [1 ];
341+ dis ++;
342+ }
343+ x -= dir [0 ];
344+ y -= dir [1 ];
345+ int d = distances [md [0 ]][md [1 ]] + dis ;
346+ if (d < distances [x ][y ]) {
347+ distances [x ][y ] = d ;
348+ pq .add (new int []{x , y , d });
349+ }
350+ }
351+ }
352+ }
262353
263354}
0 commit comments