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
- 95 lines of C++ from the credited upstream file 2258.cpp.
- 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:3 int maximumMinutes(vector<vector<int>>& grid) {4 const int kMax = grid.size() * grid[0].size();5 vector<vector<int>> fireMinute(grid.size(),6 vector<int>(grid[0].size(), -1));7 buildFireGrid(grid, fireMinute);8 9 int ans = -1;10 int l = 0;11 int r = kMax;12 13 while (l <= r) {14 const int m = (l + r) / 2;15 if (canStayFor(grid, fireMinute, m)) {16 ans = m;17 l = m + 1;18 } else {19 r = m - 1;20 }21 }22 23 return ans == kMax ? 1'000'000'000 : ans;24 }25 26 private:27 static constexpr int kDirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};28 29 void buildFireGrid(const vector<vector<int>>& grid,30 vector<vector<int>>& fireMinute) {31 queue<pair<int, int>> q;32 33 for (int i = 0; i < grid.size(); ++i)34 for (int j = 0; j < grid[0].size(); ++j)35 if (grid[i][j] == 1) { 36 q.emplace(i, j);37 fireMinute[i][j] = 0;38 }39 40 for (int minuteFromFire = 1; !q.empty(); ++minuteFromFire)41 for (int sz = q.size(); sz > 0; --sz) {42 const auto [i, j] = q.front();43 q.pop();44 for (const auto& [dx, dy] : kDirs) {45 const int x = i + dx;46 const int y = j + dy;47 if (x < 0 || x == grid.size() || y < 0 || y == grid[0].size())48 continue;49 if (grid[x][y] == 2) 50 continue;51 if (fireMinute[x][y] != -1)52 continue;53 fireMinute[x][y] = minuteFromFire;54 q.emplace(x, y);55 }56 }57 }58 59 bool canStayFor(const vector<vector<int>>& grid,60 const vector<vector<int>>& fireMinute, int minute) {61 queue<pair<int, int>> q{{{0, 0}}}; 62 vector<vector<bool>> seen(grid.size(), vector<bool>(grid[0].size()));63 seen[0][0] = true;64 65 while (!q.empty()) {66 ++minute;67 for (int sz = q.size(); sz > 0; --sz) {68 const auto [i, j] = q.front();69 q.pop();70 for (const auto& [dx, dy] : kDirs) {71 const int x = i + dx;72 const int y = j + dy;73 if (x < 0 || x == grid.size() || y < 0 || y == grid[0].size())74 continue;75 if (grid[x][y] == 2) 76 continue;77 if (x == grid.size() - 1 && y == grid[0].size() - 1) {78 if (fireMinute[x][y] != -1 && fireMinute[x][y] < minute)79 continue;80 return true;81 }82 if (fireMinute[x][y] != -1 && fireMinute[x][y] <= minute)83 continue;84 if (seen[x][y])85 continue;86 q.emplace(x, y);87 seen[x][y] = true;88 }89 }90 }91 92 return false;93 }94};95