1020. 飞地的数量
C代码:DFS
void dfs (int** grid, int x, int y, int m, int n) {
if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == 0) {
return;
}
grid[x][y] = 0;
dfs(grid, x + 1, y, m, n);
dfs(grid, x - 1, y, m, n);
dfs(grid, x, y + 1, m, n);
dfs(grid, x, y - 1, m, n);
}
int numEnclaves(int** grid, int gridSize, int* gridColSize){
int m = gridSize;
int n = gridColSize[0];
for (int i = 0; i < m; ++i) {
dfs(grid, i, 0, m, n);
dfs(grid, i, n - 1, m, n);
}
for (int j = 1; j < n - 1; ++j) {
dfs(grid, 0, j, m, n);
dfs(grid, m - 1, j, m, n);
}
int cnt = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
++cnt;
}
}
}
return cnt;
}