voidbfs(vector<vector<int>>& h, constint i, constint j, vector<vector<bool>>& visited){ q.emplace(pii{i, j});
while (!q.empty()) { constauto [y, x] = q.front(); q.pop();
if (visited[y][x]) { continue; }
visited[y][x] = true;
for (constauto& [dirY, dirX] : dirs) { constint r = y + dirY, c = x + dirX; if (!outOfBound(r, c) && h[y][x] <= h[r][c] && !visited[r][c]) { q.emplace(pii{r, c}); } } } }
booloutOfBound(constint r, constint c){ if (r < 0 || r == m || c < 0 || c == n) { returntrue; } returnfalse; } };
for (int i = 0; i < m; ++i) { bfs(heights, i, 0, pac); // left bfs(heights, i, n - 1, atl); // right }
for (int j = 0; j < n; ++j) { bfs(heights, 0, j, pac); // top bfs(heights, m - 1, j, atl); // bottom }
vector<vector<int>> res; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (pac[i][j] && atl[i][j]) { res.emplace_back(vector<int>{i, j}); } } } return res; }
private: int m, n; queue<pii> q; vector<pii> dirs = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} };
voidbfs(vector<vector<int>>& h, constint i, constint j, vector<vector<bool>>& visited){ q.emplace(pii{i, j});
while (!q.empty()) { constauto [y, x] = q.front(); q.pop();
if (outOfBound(y, x) || visited[y][x]) { continue; }
visited[y][x] = true;
for (constauto& [dirY, dirX] : dirs) { constint r = y + dirY, c = x + dirX; if (!outOfBound(r, c) && h[y][x] <= h[r][c]) { q.emplace(pii{r, c}); } } } }
booloutOfBound(constint r, constint c){ if (r < 0 || r == m || c < 0 || c == n) { returntrue; } returnfalse; } };