力扣第200题 岛屿数量
class Solution {
public:
int numIslands(vector<vector<char>>& grid)
{
int r[4]{0, 0, -1, 1};
int l[4]{ -1, 1, 0, 0 };
int res = 0;
int len1 = grid.size();
if (!len1)
return res;
int len2 = grid[0].size();
queue<pair<int, int>> q;
for (int i = 0; i < len1; i++)
{
for (int j = 0; j < len2; j++)
{
if (grid[i][j] == '1')
{
q.push(pair<int, int>(i, j));
grid[i][j] = '0';
while (!q.empty())
{
pair<int, int> pr = q.front();
q.pop();
for (int k = 0; k < 4; k++)
{
int idx1 = pr.first + r[k];
int idx2 = pr.second + l[k];
if (idx1 >= 0 && idx1 < len1 && idx2 >= 0 && idx2 < len2 && grid[idx1][idx2] == '1')
{
q.push(pair<int, int>(idx1, idx2));
grid[idx1][idx2] = '0';
}
}
}
res++;
}
}
}
return res;
}
};