@@ -64,21 +64,20 @@ public int shortestDistance(int[][] maze, int[] start, int[] destination) {
64
64
65
65
while (!queue .isEmpty ()) {
66
66
Point curr = queue .poll ();
67
- int x = curr .x , y = curr .y , distance = curr .distance ;//keep the original value
68
- if (length [x ][y ] <= distance ) continue ;
69
- length [x ][y ] = distance ;
67
+ if (length [curr .x ][curr .y ] <= curr .distance ) continue ;
68
+ length [curr .x ][curr .y ] = curr .distance ;
70
69
for (int i = 0 ; i < directions .length - 1 ; i ++) {
71
- int xx = curr .x , yy = curr .y , distanceDistance = curr .distance ;//use temp variables to move
70
+ int x = curr .x , y = curr .y , distance = curr .distance ;//use temp variables to move
72
71
//we need below while loop to find only "stop" points that could be put into the queue
73
- while (xx >= 0 && yy >= 0 && xx < m && yy < n && maze [xx ][ yy ] == 0 ) {
74
- xx += directions [i ];
75
- yy += directions [i + 1 ];
76
- distanceDistance ++;
72
+ while (x >= 0 && y >= 0 && x < m && y < n && maze [x ][ y ] == 0 ) {
73
+ x += directions [i ];
74
+ y += directions [i + 1 ];
75
+ distance ++;
77
76
}
78
- xx -= directions [i ];
79
- yy -= directions [i + 1 ];
80
- distanceDistance --;
81
- queue .offer (new Point (xx , yy , distanceDistance ));
77
+ x -= directions [i ];
78
+ y -= directions [i + 1 ];
79
+ distance --;
80
+ queue .offer (new Point (x , y , distance ));
82
81
}
83
82
}
84
83
return length [destination [0 ]][destination [1 ]] == Integer .MAX_VALUE ? -1 : length [destination [0 ]][destination [1 ]];
0 commit comments