|
| 1 | +// https://leetcode.com/problems/the-maze-iii |
| 2 | +// T: O(m * n log(m * n)) |
| 3 | +// S: O(m * n) |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.PriorityQueue; |
| 9 | +import java.util.Queue; |
| 10 | + |
| 11 | +public class TheMazeIII { |
| 12 | + private record Info(int[] position, int distance, String path) {} |
| 13 | + |
| 14 | + private record Position(int[] position, String direction) {} |
| 15 | + |
| 16 | + private static final Position[] DIRECTIONS = new Position[] { |
| 17 | + new Position(new int[] { 1, 0 }, "d"), |
| 18 | + new Position(new int[] { 0, -1 }, "l"), |
| 19 | + new Position(new int[] { 0, 1 }, "r"), |
| 20 | + new Position(new int[] { -1, 0 }, "u") |
| 21 | + }; |
| 22 | + |
| 23 | + // T: O(m * n * log(m * n)), S: O(m * n) |
| 24 | + public String findShortestWay(int[][] maze, int[] ball, int[] hole) { |
| 25 | + final int[][] distances = getDistances(maze.length, maze[0].length); |
| 26 | + final Queue<Info> queue = new PriorityQueue<>((a, b) -> { |
| 27 | + if (a.distance == b.distance) { |
| 28 | + return a.path.compareTo(b.path); |
| 29 | + } |
| 30 | + return Integer.compare(a.distance, b.distance); |
| 31 | + }); |
| 32 | + queue.add(new Info(ball, 0, "")); |
| 33 | + String path = null; |
| 34 | + |
| 35 | + while (!queue.isEmpty()) { |
| 36 | + final Info info = queue.poll(); |
| 37 | + |
| 38 | + if (distances[info.position[0]][info.position[1]] <= info.distance) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + distances[info.position[0]][info.position[1]] = info.distance; |
| 43 | + |
| 44 | + if (info.position[0] == hole[0] && info.position[1] == hole[1]) { |
| 45 | + if (path == null) { |
| 46 | + path = info.path; |
| 47 | + } else { |
| 48 | + path = path.compareTo(info.path) > 0 ? info.path : path; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + for (Position position : validPositions(maze, info.position, hole)) { |
| 53 | + queue.add(new Info( |
| 54 | + position.position, |
| 55 | + info.distance + manhattanDistance(position.position, info.position), |
| 56 | + info.path + position.direction |
| 57 | + )); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + final int minDistance = distances[hole[0]][hole[1]]; |
| 62 | + return minDistance == Integer.MAX_VALUE ? "impossible" : path; |
| 63 | + } |
| 64 | + |
| 65 | + // T: O(1), S:O(1) |
| 66 | + private static int manhattanDistance(int[] p1, int[] p2) { |
| 67 | + return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]); |
| 68 | + } |
| 69 | + |
| 70 | + // T: O(m + n), S: O(1) |
| 71 | + private static List<Position> validPositions(int[][] grid, int[] position, int[] target) { |
| 72 | + final List<Position> result = new ArrayList<>(); |
| 73 | + for (Position p : DIRECTIONS) { |
| 74 | + int row = position[0], column = position[1]; |
| 75 | + while (row >= 0 && row < grid.length |
| 76 | + && column >= 0 && column < grid[0].length && grid[row][column] == 0 && |
| 77 | + (row != target[0] || column != target[1])) { |
| 78 | + row += p.position[0]; |
| 79 | + column += p.position[1]; |
| 80 | + } |
| 81 | + if (row == target[0] && column == target[1]) { |
| 82 | + result.add(new Position(new int[] { row, column}, p.direction)); |
| 83 | + } else { |
| 84 | + result.add(new Position(new int[]{row - p.position[0], column - p.position[1]}, p.direction)); |
| 85 | + } |
| 86 | + } |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + // T: O(m * n), S: O(m * n) |
| 91 | + private static int[][] getDistances(int rows, int columns) { |
| 92 | + final int[][] distances = new int[rows][columns]; |
| 93 | + for (int[] row : distances) { |
| 94 | + Arrays.fill(row, Integer.MAX_VALUE); |
| 95 | + } |
| 96 | + return distances; |
| 97 | + } |
| 98 | +} |
0 commit comments