boolcheck(vector<vector<int>> &grid, int t){ int m = grid.size(), n = grid[0].size(); vector<vector<int>> on_fire(m, vector<int>(n)); vector<pair<int, int>> f; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == 1) { on_fire[i][j] = true; f.emplace_back(i, j); } } } // 火的 BFS auto spread_fire = [&]() { vector<pair<int, int>> nf; for (auto &[i, j]: f) { for (auto &[dx, dy]: dirs) { int x = i + dx, y = j + dy; if (0 <= x && x < m && 0 <= y && y < n && !on_fire[x][y] && grid[x][y] == 0) { on_fire[x][y] = true; nf.emplace_back(x, y); } } } f = move(nf); }; while (t-- && !f.empty()) { spread_fire(); } if (on_fire[0][0]) { returnfalse; }
vector<vector<int>> vis(m, vector<int>(n)); vis[0][0] = true; vector<pair<int, int>> q{{0, 0}}; while (!q.empty()) { vector<pair<int, int>> nq; for (auto &[i, j]: q) { if (on_fire[i][j]) continue; for (auto &[dx, dy]: dirs) { int x = i + dx, y = j + dy; if (0 <= x && x < m && 0 <= y && y < n && !on_fire[x][y] && !vis[x][y] && grid[x][y] == 0) { if (x == m - 1 && y == n - 1) { returntrue; } vis[x][y] = true; nq.emplace_back(x, y); } } } q = move(nq); spread_fire(); } returnfalse; }
public: intmaximumMinutes(vector<vector<int>> &grid){ int m = grid.size(), n = grid[0].size(); int left = -1, right = m * n + 1; while (left + 1 < right) { int mid = (left + right) / 2; (check(grid, mid) ? left : right) = mid; } return left < m * n ? left : 1'000'000'000; } };