-
Notifications
You must be signed in to change notification settings - Fork 0
/
200. Number of Islands 2.cpp
64 lines (53 loc) · 1.56 KB
/
200. Number of Islands 2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// https://leetcode.com/problems/number-of-islands/
class Solution
{
public:
int numIslands(vector<vector<char>> &grid)
{
int result = 0;
for (int i = 0; i < grid.size(); ++i)
{
for (int j = 0; j < grid[i].size(); ++j)
{
if (grid[i][j] == '1')
{
result++;
bfs(grid, i, j);
}
}
}
return result;
}
void bfs(vector<vector<char>> &grid, int i, int j)
{
queue<pair<int, int>> nodes;
vector<vector<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
nodes.push({i, j});
grid[i][j] = '0';
while (!nodes.empty())
{
int size = nodes.size();
for (int k = 0; k < size; ++k)
{
pair<int, int> current = nodes.front();
nodes.pop();
for (auto &direction : directions)
{
int row = current.first + direction[0];
int column = current.second + direction[1];
if (isValidPosition(grid, row, column))
{
nodes.push({row, column});
grid[row][column] = '0';
}
}
}
}
}
bool isValidPosition(vector<vector<char>> &grid, int i, int j)
{
if (i >= grid.size() || j >= grid[0].size() || i < 0 || j < 0 || grid[i][j] == '0')
return false;
return true;
}
};