1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| public void dijkstra(char[][] grid) { int m = grid.length; int n = grid[0].length; if (m == 0 || n == 0) { return; }
int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
int[][] dists = new int[m][n]; for (int i = 0; i < dists.length; i++) { Arrays.fill(dists[i], Integer.MAX_VALUE); } dists[0][0] = 1;
int[][][] parents = new int[m][n][2];
boolean[][] visited = new boolean[m][n]; visited[0][0] = true;
Queue<int[]> qn = new LinkedList<>(); qn.add(new int[]{0, 0});
while (!qn.isEmpty()) { int[] node = qn.poll(); int i = node[0]; int j = node[1]; visited[i][j] = true; for (int dir = 0; dir < dirs.length; dir++) { int ni = i + dirs[dir][0]; int nj = j + dirs[dir][1]; if (ni >= 0 && ni < m && nj >= 0 && nj < n ) { if (visited[ni][nj] == false && dists[ni][nj] > dists[i][j] + 1 ) { qn.add(new int[]{ni, nj}); parents[ni][nj] = new int[]{i, j}; dists[ni][nj] = dists[i][j] + 1; } } } }
Stack<int[]> sn = new Stack<>(); sn.add(new int[]{m - 1, n - 1}); int[] parent = parents[m - 1][n - 1]; sn.add(parent); while (parent[0] != 0 || parent[1] != 0) { parent = parents[parent[0]][parent[1]]; sn.add(parent); } System.out.println("The shortest distance is " + dists[m - 1][n - 1]); while (!sn.isEmpty()) { int[] node = sn.pop(); int i = node[0]; int j = node[1]; if (sn.isEmpty()) { System.out.print("(" + i + ", " + j + ")"); } else { System.out.print("(" + i + ", " + j + "), "); } } }
|