-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberofIsLands.c
45 lines (34 loc) · 913 Bytes
/
NumberofIsLands.c
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
//first set count + 1
//map += 1
void dfs(char** grid, int i, int j, int size, int colSize) {
//youxia
grid[i][j] = 0;
//上
if (i - 1 >= 0 && grid[i - 1][j] == '1'){
dfs(grid, i - 1, j, size, colSize);
}
//下
if (i + 1 < size && grid[i + 1][j] == '1'){
dfs(grid, i + 1, j, size, colSize);
}
//左
if (j - 1 >= 0 && grid[i][j - 1] == '1'){
dfs(grid, i, j - 1, size, colSize);
}
//右
if (j + 1 < colSize && grid[i][j + 1] == '1'){
dfs(grid, i, j + 1, size, colSize);
}
return;
}
int numIslands(char** grid, int gridSize, int* gridColSize) {
int count = 0;
for (int i = 0; i < gridSize; i++)
for (int j = 0; j < *gridColSize; j++){
if (grid[i][j] == '1'){
count++;
dfs(grid, i, j, gridSize, *gridColSize);
}
}
return count;
}