力扣:200岛屿数量
C++代码:class Solution {
int dirs[4][2] = {{0,-1},{0,1},{1,0},{-1,0}};
public:
int numIslands(vector
int n = grid.size();
int m = grid[0].size();
int res = 0;
for(int i = 0;i { for(int j = 0;j { if(grid[i][j] == '1') { res++; grid[i][j] = '0'; queue q.push({i,j}); while(!q.empty()) { auto cell = q.front(); q.pop(); int r = cell.first; int c = cell.second; for(int i = 0;i<4;i++) { int X = r+dirs[i][0]; int Y = c+dirs[i][1]; if(X>=0 && X { q.push({X,Y}); grid[X][Y] = '0'; } } } } } } return res; } };



