class Solution {
public:
/**
* 判断岛屿数量
* @param grid char字符型vector<vector<>>
* @return int整型
*/
/*
有一个为1的时候就被周边所有的1都置成0,就产生了隔离。有点像朋友圈。
*/
void dfs(vector<vector<char> >& grid, int x, int y) {
int column = grid.size(), line = grid[0].size();
if (x < 0 || x >= column || y < 0 || y >= line) {
return;
}
if (grid[x][y] == '0') {
return;
}
grid[x][y] = '0';
dfs(grid, x+1, y);
dfs(grid, x-1, y);
dfs(grid, x, y + 1);
dfs(grid, x, y-1);
}
int solve(vector<vector<char> >& grid) {
int column = grid.size(), line = grid[0].size();
vector<vector<bool>> flag(column, vector<bool>(line, false));
int cnt = 0;
for (int i = 0; i < column; i++) {
for (int j = 0; j < line; j++) {
if (grid[i][j] == '1') {
cnt++;
dfs(grid, i, j);
}
}
}
return cnt;
// write code here
}
};