Approach
Breadth-first search
For Escape the Spreading Fire, 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
- 92 lines of Java from the credited upstream file 2258.java.
- The implementation visibly relies on sequence storage, work queue.
- 9 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 public int maximumMinutes(int[][] grid) {3 final int MAX = grid.length * grid[0].length;4 int[][] fireMinute = new int[grid.length][grid[0].length];5 Arrays.stream(fireMinute).forEach(A -> Arrays.fill(A, -1));6 buildFireGrid(grid, fireMinute);7 8 int ans = -1;9 int l = 0;10 int r = MAX;11 12 while (l <= r) {13 final int m = (l + r) / 2;14 if (canStayFor(grid, fireMinute, m)) {15 ans = m;16 l = m + 1;17 } else {18 r = m - 1;19 }20 }21 22 return ans == MAX ? 1_000_000_000 : ans;23 }24 25 private static final int[][] DIRS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};26 27 private void buildFireGrid(int[][] grid, int[][] fireMinute) {28 Queue<Pair<Integer, Integer>> q = new ArrayDeque<>();29 30 for (int i = 0; i < grid.length; ++i)31 for (int j = 0; j < grid[0].length; ++j)32 if (grid[i][j] == 1) { 33 q.offer(new Pair<>(i, j));34 fireMinute[i][j] = 0;35 }36 37 for (int minuteFromFire = 1; !q.isEmpty(); ++minuteFromFire)38 for (int sz = q.size(); sz > 0; --sz) {39 final int i = q.peek().getKey();40 final int j = q.poll().getValue();41 for (int[] dir : DIRS) {42 final int x = i + dir[0];43 final int y = j + dir[1];44 if (x < 0 || x == grid.length || y < 0 || y == grid[0].length)45 continue;46 if (grid[x][y] == 2) 47 continue;48 if (fireMinute[x][y] != -1)49 continue;50 fireMinute[x][y] = minuteFromFire;51 q.offer(new Pair<>(x, y));52 }53 }54 }55 56 boolean canStayFor(int[][] grid, int[][] fireMinute, int minute) {57 Queue<Pair<Integer, Integer>> q =58 new ArrayDeque<>(List.of(new Pair<>(0, 0))); 59 boolean[][] seen = new boolean[grid.length][grid[0].length];60 seen[0][0] = true;61 62 while (!q.isEmpty()) {63 ++minute;64 for (int sz = q.size(); sz > 0; --sz) {65 final int i = q.peek().getKey();66 final int j = q.poll().getValue();67 for (int[] dir : DIRS) {68 final int x = i + dir[0];69 final int y = j + dir[1];70 if (x < 0 || x == grid.length || y < 0 || y == grid[0].length)71 continue;72 if (grid[x][y] == 2) 73 continue;74 if (x == grid.length - 1 && y == grid[0].length - 1) {75 if (fireMinute[x][y] != -1 && fireMinute[x][y] < minute)76 continue;77 return true;78 }79 if (fireMinute[x][y] != -1 && fireMinute[x][y] <= minute)80 continue;81 if (seen[x][y])82 continue;83 q.offer(new Pair<>(x, y));84 seen[x][y] = true;85 }86 }87 }88 89 return false;90 }91}92