using
System;
using
System.Collections.Generic;
class
GFG {
static
int
ROW = 4;
static
int
COL = 5;
static
bool
isSafe(
int
[, ] M,
int
row,
int
col,
bool
[, ] visited)
{
return
(row >= 0) && (row < ROW) && (col >= 0)
&& (col < COL)
&& (M[row, col] == 1 && !visited[row, col]);
}
static
void
DFS(
int
[, ] M,
int
row,
int
col,
bool
[, ] visited,
ref
int
count)
{
int
[] rowNbr = { -1, -1, -1, 0, 0, 1, 1, 1 };
int
[] colNbr = { -1, 0, 1, -1, 1, -1, 0, 1 };
visited[row, col] =
true
;
for
(
int
k = 0; k < 8; ++k) {
if
(isSafe(M, row + rowNbr[k], col + colNbr[k],
visited)) {
count++;
DFS(M, row + rowNbr[k], col + colNbr[k],
visited,
ref
count);
}
}
}
static
void
commonRegion(
int
[, ] M)
{
bool
[, ] visited =
new
bool
[ROW, COL];
for
(
int
i = 0; i < ROW; ++i) {
for
(
int
j = 0; j < COL; ++j) {
visited[i, j] =
false
;
}
}
Dictionary<
int
, List<List<
int
> > > um
=
new
Dictionary<
int
, List<List<
int
> > >();
for
(
int
i = 0; i < ROW; ++i) {
for
(
int
j = 0; j < COL; ++j) {
if
(M[i, j] == 1 && !visited[i, j]) {
int
count = 1;
DFS(M, i, j, visited,
ref
count);
if
(!um.ContainsKey(count)) {
um[count] =
new
List<List<
int
> >();
}
um[count].Add(
new
List<
int
>{ i, j });
}
}
}
int
mostCommonRegion = 0;
foreach
(KeyValuePair<
int
, List<List<
int
> > > entry
in
um)
{
int
x = entry.Value.Count;
mostCommonRegion
= Math.Max(mostCommonRegion, x);
}
foreach
(KeyValuePair<
int
, List<List<
int
> > > entry
in
um)
{
int
x = entry.Value.Count;
if
(mostCommonRegion == x) {
foreach
(List<
int
> list
in
entry.Value)
{
int
x1 = list[0];
int
y1 = list[1];
Console.Write(
"{"
+ x1 +
", "
+ y1
+
"}, "
);
}
break
;
}
}
}
static
void
Main()
{
int
[, ] arr = { { 0, 0, 1, 1, 0 },
{ 1, 0, 1, 1, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1 } };
commonRegion(arr);
}
}