add: 230406

This commit is contained in:
Eatswap 2023-04-06 19:29:10 +08:00
parent e5a7e1f172
commit dd230c20a5
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 61 additions and 1 deletions

60
cpp/2304/230406.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
/**
* 1254. Number of Closed Islands
*
* Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.
* Return the number of closed islands.
*/
class Solution {
private:
static const inline int dX[] = {0, 1, 0, -1}, dY[] = {1, 0, -1, 0};
public:
static int closedIsland(std::vector<std::vector<int>>&);
};
int Solution::closedIsland(std::vector<std::vector<int>>& G) {
const int m = G.size(), n = G.front().size();
auto setAs1 = [&](int x, int y) {
if (G[x][y]) return 0;
std::queue<std::pair<int, int>> q;
q.emplace(x, y);
for (int nx, ny; !q.empty(); q.pop()) {
auto&& [cx, cy] = q.front();
G[cx][cy] = 1;
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)
q.emplace(nx, ny);
}
}
return 1;
};
for (int i = 0; i < n; ++i)
setAs1(0, i), setAs1(m - 1, i);
for (int i = 1; i < m; ++i)
setAs1(i, 0), setAs1(i, n - 1);
int ret = 0;
for (int i = 1; i < m - 1; ++i)
for (int j = 1; j < n - 1; ++j)
ret += setAs1(i, j);
return ret;
}
int main() {
std::vector<std::vector<int>> a {
{1,1,1,1,1,1,1,0},
{1,0,0,0,0,1,1,0},
{1,0,1,0,1,1,1,0},
{1,0,0,0,0,1,0,1},
{1,1,1,1,1,1,1,0}
};
std::cout << Solution::closedIsland(a);
return 0;
}

View File

@ -4,4 +4,4 @@ PROJECT(2304)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
SET(CMAKE_EXPORT_COMPILE_COMMANDS true) SET(CMAKE_EXPORT_COMPILE_COMMANDS true)
ADD_EXECUTABLE(2304 230406-CN.cpp) ADD_EXECUTABLE(2304 230406.cpp)