diff --git a/cpp/2304/230406.cpp b/cpp/2304/230406.cpp index eb2a7a3..4896769 100644 --- a/cpp/2304/230406.cpp +++ b/cpp/2304/230406.cpp @@ -20,6 +20,7 @@ public: int Solution::closedIsland(std::vector>& G) { const int m = G.size(), n = G.front().size(); + std::vector vis(m * n); auto setAs1 = [&](int x, int y) { if (G[x][y]) return 0; std::queue> q; @@ -30,8 +31,10 @@ int Solution::closedIsland(std::vector>& G) { for (int i = 0; i < 4; ++i) { nx = cx + dX[i]; ny = cy + dY[i]; - if (nx >= 0 && nx < m && ny >= 0 && ny < n && G[nx][ny] == 0) + if (nx >= 0 && nx < m && ny >= 0 && ny < n && G[nx][ny] == 0 && !vis[nx * n + ny]) { q.emplace(nx, ny); + vis[nx * n + ny] = true; + } } } return 1;