-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIland.cs
More file actions
45 lines (36 loc) · 1005 Bytes
/
Iland.cs
File metadata and controls
45 lines (36 loc) · 1005 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
37
38
39
40
41
42
43
44
45
using System;
class Iland
{
public static int noOfIlands(char[,] grid)
{
int count =0;
for (int i = 0; i <= grid.Rank; i++)
{
for (int j = 0; j <= grid.Rank; j++)
{
if (grid[i, j] == '1')
{
count++;
callBFS(grid, i, j);
}
}
}
return count;
}
public static void callBFS(char[,] grid, int i, int j)
{
if (i < 0 || i >= grid.Rank || j < 0 || j >= grid.Rank || grid[i, j] == '0')
return;
grid[i, j] = '0';
callBFS(grid, i + 1, j);
callBFS(grid, i - 1, j);
callBFS(grid, i, j + 1);
callBFS(grid, i, j - 1);
}
public static void MainC(string[] args)
{
char[,] A = new char[,] { { '1', '1', '0' }, { '1', '0', '1' }, { '0', '0', '0' } };
int count = noOfIlands(A);
Console.WriteLine("No of Ilands are " + count);
}
}