using
System;
using
System.Collections.Generic;
public
class
Pair {
public
int
a, b, c;
public
Pair(
int
a,
int
b,
int
c)
{
this
.a = a;
this
.b = b;
this
.c = c;
}
}
public
class
Program {
public
static
List<List<
int
> >
ChefAndWells(
int
n,
int
m, List<List<
char
> > c)
{
List<List<
int
> > ans =
new
List<List<
int
> >();
for
(
int
i = 0; i < n; i++) {
ans.Add(
new
List<
int
>());
for
(
int
j = 0; j < m; j++) {
ans[i].Add(
int
.MaxValue);
}
}
List<List<
bool
> > v =
new
List<List<
bool
> >();
for
(
int
i = 0; i < n; i++) {
v.Add(
new
List<
bool
>());
for
(
int
j = 0; j < m; j++) {
v[i].Add(
false
);
}
}
Queue<Pair> q =
new
Queue<Pair>();
for
(
int
i = 0; i < n; i++) {
for
(
int
j = 0; j < m; j++) {
if
(c[i][j] ==
'W'
) {
v[i][j] =
true
;
ans[i][j] = 0;
q.Enqueue(
new
Pair(i, j, 0));
}
}
}
int
[] dx = { 0, 0, -1, 1 };
int
[] dy = { -1, 1, 0, 0 };
while
(q.Count > 0) {
Pair p = q.Dequeue();
for
(
int
i = 0; i < 4; i++) {
int
nx = p.a + dx[i];
int
ny = p.b + dy[i];
if
(nx >= 0 && nx < n && ny >= 0 && ny < m
&& !v[nx][ny] && c[nx][ny] !=
'N'
) {
q.Enqueue(
new
Pair(nx, ny, p.c + 1));
v[nx][ny] =
true
;
ans[nx][ny] = p.c + 1;
}
}
}
for
(
int
i = 0; i < n; i++) {
for
(
int
j = 0; j < m; j++) {
if
(c[i][j] ==
'.'
|| c[i][j] ==
'N'
) {
ans[i][j] = 0;
}
else
if
(ans[i][j] ==
int
.MaxValue) {
ans[i][j] = -1;
}
else
{
ans[i][j] *= 2;
}
}
}
return
ans;
}
public
static
void
Main(
string
[] args)
{
int
n = 3;
int
m = 3;
List<List<
char
> > c =
new
List<List<
char
> >() {
new
List<
char
>{
'H'
,
'H'
,
'H'
},
new
List<
char
>{
'H'
,
'W'
,
'H'
},
new
List<
char
>
{
'H'
,
'H'
,
'H'
}
};
List<List<
int
> > ans = ChefAndWells(n, m, c);
for
(
int
i = 0; i < n; i++) {
for
(
int
j = 0; j < m; j++) {
Console.Write(ans[i][j] +
" "
);
}
Console.WriteLine();
}
}
}