class Solution { public: /** * 判断岛屿数量 * @param grid char字符型vector<vector<>> * @return int整型 */ int nums; vector<vector<char> > grid; void dfs(int x,int y){ grid[x][y]='0'; if(x+1<grid.size()&&grid[x+1][y]=='1'){ dfs(x+1,y); } if(x-1>=0&&grid[x-1][y]=='1'){ dfs(x-1,y); } if(y+1<grid[0].size()&&grid[x][y+1]=='1'){ dfs(x,y+1); } if(y-1>=0&&grid[x][y-1]=='1'){ dfs(x,y-1); } } int solve(vector<vector<char> >& _grid) { nums=0; if(_grid.size()==0) return nums; this->grid=_grid; for(int i=0;i<grid.size();i++){ for(int j=0;j<grid[0].size();j++){ if(grid[i][j]=='1'){ dfs(i,j); nums++; } } } return nums; } };