Skip to content

Commit b6b6f70

Browse files
[LEET-505] edit 505
1 parent 8f39df3 commit b6b6f70

File tree

1 file changed

+11
-12
lines changed
  • leetcode-algorithms/src/main/java/com/stevesun/solutions

1 file changed

+11
-12
lines changed

leetcode-algorithms/src/main/java/com/stevesun/solutions/TheMazeII.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,20 @@ public int shortestDistance(int[][] maze, int[] start, int[] destination) {
6464

6565
while (!queue.isEmpty()) {
6666
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;
7069
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
7271
//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++;
7776
}
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));
8281
}
8382
}
8483
return length[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : length[destination[0]][destination[1]];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy