forked from algorithm001/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_200_34.cs
More file actions
40 lines (37 loc) · 1.03 KB
/
Copy pathLeetCode_200_34.cs
File metadata and controls
40 lines (37 loc) · 1.03 KB
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
public class Solution {
public int NumIslands(char[][] grid)
{
int islandCount = 0;
for (int y = 0; y < grid.Length; y++)
{
for (int x = 0; x < grid[y].Length; x++)
{
if (grid[y][x] == '1')
{
TraceIsland(grid, y, x);
islandCount++;
}
}
}
return islandCount;
}
public void TraceIsland(char[][] grid, int y, int x)
{
if (y < 0 || x < 0 || y >= grid.Length || x >= grid[y].Length)
{
return;
}
if (grid[y][x] == '1')
{
grid[y][x] = '2';
TraceIsland(grid, y + 1, x);
TraceIsland(grid, y - 1, x);
TraceIsland(grid, y, x + 1);
TraceIsland(grid, y, x - 1);
}
else
{
return;
}
}
}