using
System;
using
System.Collections.Generic;
public
class
GFG
{
static
int
dist(Pair p1, Pair p2)
{
return
Math.Abs(p1.first - p2.first)
+ Math.Abs(p2.second - p1.second) - 1;
}
class
Pair {
public
int
first;
public
int
second;
public
Pair(
int
f,
int
s)
{
this
.first = f;
this
.second = s;
}
}
static
void
floodfill(HashSet<Pair> hash,
int
i,
int
j,
char
[,] A)
{
if
(i < 0 || i >= A.GetLength(0) || j < 0
|| j >= A.GetLength(1) || A[i,j] !=
'L'
) {
return
;
}
A[i,j] =
'V'
;
hash.Add(
new
Pair(i, j));
floodfill(hash, i - 1, j, A);
floodfill(hash, i + 1, j, A);
floodfill(hash, i, j - 1, A);
floodfill(hash, i, j + 1, A);
}
static
void
findMinimumW(
char
[,] A)
{
if
(A.GetLength(0) == 0)
return
;
HashSet<Pair> hash1 =
new
HashSet<Pair>();
HashSet<Pair> hash2 =
new
HashSet<Pair>();
for
(
int
i = 0; i < A.GetLength(0); i++) {
for
(
int
j = 0; j < A.GetLength(1); j++) {
if
(A[i,j] ==
'L'
) {
if
(hash1.Count==0) {
floodfill(hash1, i, j, A);
}
if
(hash2.Count==0
&& !hash1.Contains(
new
Pair(i, j))) {
floodfill(hash2, i, j, A);
}
}
}
}
int
ans =
int
.MaxValue;
foreach
(Pair i
in
hash1) {
foreach
(Pair j
in
hash2) {
ans = Math.Min(ans, dist(i, j));
}
}
Console.WriteLine(ans);
}
public
static
void
Main(String[] args)
{
char
[,] arr = { {
'W'
,
'L'
}, {
'L'
,
'W'
} };
findMinimumW(arr);
}
}