Approach
Breadth-first search
For Grid Teleportation Traversal, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 76 lines of Java from the credited upstream file 3552.java.
- The implementation visibly relies on sequence storage, work queue.
- 6 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 3 public int minMoves(String[] matrix) {4 if (matrix[matrix.length - 1].charAt(matrix[0].length() - 1) == '#')5 return -1;6 7 List<Pair<Integer, Integer>>[] teleportPositions = new ArrayList[26];8 9 for (int i = 0; i < 26; ++i)10 teleportPositions[i] = new ArrayList<>();11 12 for (int i = 0; i < matrix.length; ++i)13 for (int j = 0; j < matrix[0].length(); ++j) {14 final char c = matrix[i].charAt(j);15 if (c != '.' && c != '#')16 teleportPositions[c - 'A'].add(new Pair<>(i, j));17 }18 19 return dijkstra(matrix, teleportPositions, new Pair<>(0, 0),20 new Pair<>(matrix.length - 1, matrix[0].length() - 1));21 }22 23 private int dijkstra(String[] matrix, List<Pair<Integer, Integer>>[] teleportPositions,24 Pair<Integer, Integer> src, Pair<Integer, Integer> dst) {25 final int[][] DIRS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};26 final int m = matrix.length;27 final int n = matrix[0].length();28 int[][] dist = new int[m][n];29 Arrays.stream(dist).forEach(A -> Arrays.fill(A, Integer.MAX_VALUE));30 boolean[] seen = new boolean[26];31 32 dist[0][0] = 0;33 Queue<Pair<Integer, Pair<Integer, Integer>>> minHeap =34 new PriorityQueue<>(Comparator.comparingInt(Pair::getKey)) {35 { offer(new Pair<>(dist[0][0], src)); } 36 };37 38 while (!minHeap.isEmpty()) {39 final int d = minHeap.peek().getKey();40 final Pair<Integer, Integer> u = minHeap.poll().getValue();41 if (u.equals(dst))42 return d;43 final int i = u.getKey();44 final int j = u.getValue();45 if (d > dist[i][j])46 continue;47 final char c = matrix[i].charAt(j);48 if (Character.isUpperCase(c) && !seen[c - 'A']) {49 seen[c - 'A'] = true;50 for (Pair<Integer, Integer> pos : teleportPositions[c - 'A']) {51 final int x = pos.getKey();52 final int y = pos.getValue();53 if (d < dist[x][y]) {54 dist[x][y] = d;55 minHeap.offer(new Pair<>(d, new Pair<>(x, y)));56 }57 }58 }59 for (int[] dir : DIRS) {60 final int x = i + dir[0];61 final int y = j + dir[1];62 if (x < 0 || x == m || y < 0 || y == n)63 continue;64 if (matrix[x].charAt(y) == '#')65 continue;66 if (d + 1 < dist[x][y]) {67 dist[x][y] = d + 1;68 minHeap.offer(new Pair<>(d + 1, new Pair<>(x, y)));69 }70 }71 }72 73 return -1;74 }75}76