-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path200-gusrb3164.cpp
More file actions
36 lines (34 loc) · 891 Bytes
/
200-gusrb3164.cpp
File metadata and controls
36 lines (34 loc) · 891 Bytes
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
class Solution
{
public:
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int numIslands(vector<vector<char>> &grid)
{
int result = 0;
for (int y = 0; y < grid.size(); y++)
{
for (int x = 0; x < grid[0].size(); x++)
{
if (findIsland(x, y, grid))
{
result++;
}
}
}
return result;
}
bool findIsland(int x, int y, vector<vector<char>> &grid)
{
if (x < 0 || x >= grid[0].size() || y < 0 || y >= grid.size() || grid[y][x] != '1')
{
return false;
}
grid[y][x] = '0'; //이제 방문한 지역이므로 중복 안되게 0으로 바꿔줌
for (int i = 0; i < 4; i++)
{
findIsland(x + dx[i], y + dy[i], grid);
}
return true;
}
};