#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int Nmax = 404;
int cells, n, m;
bool bad;
char a[Nmax][Nmax];
int ans[Nmax][Nmax];
int Upper[Nmax][Nmax], Lower[Nmax][Nmax];
void add_upper(int x, int y);
void add_lower(int x, int y)
{
if(Lower[x][y] == 1) bad = 1;
if(Lower[x][y] || bad) return;
Lower[x][y] = 1;
++cells;
if(a[x][y] == 'N') /// adaug dreapta
{
if(a[x][y+1] == 'N')
add_lower(x, y+1);
else if(a[x][y+1] == 'Z')
add_upper(x, y+1);
if(x - 1 >= 1)
add_lower(x-1, y);
}
else
{
if(a[x][y-1] == 'N')
add_upper(x, y-1);
else if(a[x][y-1] == 'Z')
add_lower(x, y-1);
if(x - 1 >= 1)
add_lower(x-1, y);
}
Lower[x][y] = -1;
}
void add_upper(int x, int y)
{
if(Upper[x][y] == 1) bad = 1;
if(Upper[x][y] || bad) return;
Upper[x][y] = 1;
++cells;
if(a[x][y] == 'N') /// adaug dreapta
{
if(a[x][y-1] == 'N')
add_upper(x, y-1);
else if(a[x][y-1] == 'Z')
add_lower(x, y-1);
if(x + 1 <= n)
add_upper(x+1, y);
}
else
{
if(a[x][y+1] == 'N')
add_lower(x, y+1);
else if(a[x][y+1] == 'Z')
add_upper(x, y+1);
if(x + 1 <= n)
add_upper(x+1, y);
}
Upper[x][y] = -1;
}
void solve1(int c)
{
int i, j;
for(i=1; i<=n; ++i)
for(j=1; j<=m; ++j)
Lower[i][j] = Upper[i][j] = 0;
cells = 0;
bad = 0;
for(i=1; i<=n; ++i)
{
add_lower(i, c);
if(bad) return;
ans[i][c] = min(ans[i][c], cells);
}
}
void solve2(int c)
{
int i, j;
for(i=1; i<=n; ++i)
for(j=1; j<=m; ++j)
Lower[i][j] = Upper[i][j] = 0;
cells = 0;
bad = 0;
for(i=n; i; --i)
{
add_upper(i, c);
if(bad) return;
ans[i][c] = min(ans[i][c], cells);
}
}
int main()
{
// freopen("input", "r", stdin);
cin.tie(0); cin.sync_with_stdio(false);
cin >> n >> m;
int i, j;
for(i=1; i<=n; ++i) cin >> (a[i] + 1);
for(i=1; i<=n; ++i)
for(j=1; j<=m; ++j)
ans[i][j] = inf;
for(i=1; i<=m; ++i)
solve1(i);
for(i=1; i<=m; ++i)
solve2(i);
for(i=1; i<=n; ++i)
for(j=1; j<=m; ++j)
cout << (ans[i][j] == inf ? -1 : 2 * ans[i][j]) << " \n"[j==m];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
10 ms |
640 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
8 ms |
640 KB |
Output is correct |
14 |
Correct |
9 ms |
640 KB |
Output is correct |
15 |
Correct |
3 ms |
640 KB |
Output is correct |
16 |
Correct |
4 ms |
640 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
4 ms |
640 KB |
Output is correct |
19 |
Correct |
5 ms |
660 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
640 KB |
Output is correct |
24 |
Correct |
8 ms |
640 KB |
Output is correct |
25 |
Correct |
8 ms |
640 KB |
Output is correct |
26 |
Correct |
6 ms |
640 KB |
Output is correct |
27 |
Correct |
4 ms |
640 KB |
Output is correct |
28 |
Correct |
6 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
10 ms |
640 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
8 ms |
640 KB |
Output is correct |
14 |
Correct |
9 ms |
640 KB |
Output is correct |
15 |
Correct |
3 ms |
640 KB |
Output is correct |
16 |
Correct |
4 ms |
640 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
4 ms |
640 KB |
Output is correct |
19 |
Correct |
5 ms |
660 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
640 KB |
Output is correct |
24 |
Correct |
8 ms |
640 KB |
Output is correct |
25 |
Correct |
8 ms |
640 KB |
Output is correct |
26 |
Correct |
6 ms |
640 KB |
Output is correct |
27 |
Correct |
4 ms |
640 KB |
Output is correct |
28 |
Correct |
6 ms |
768 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
2432 KB |
Output is correct |
31 |
Correct |
188 ms |
3064 KB |
Output is correct |
32 |
Correct |
184 ms |
3192 KB |
Output is correct |
33 |
Correct |
19 ms |
2304 KB |
Output is correct |
34 |
Correct |
939 ms |
3576 KB |
Output is correct |
35 |
Correct |
1093 ms |
3064 KB |
Output is correct |
36 |
Correct |
3 ms |
2432 KB |
Output is correct |
37 |
Correct |
1369 ms |
3548 KB |
Output is correct |
38 |
Correct |
1649 ms |
3448 KB |
Output is correct |
39 |
Correct |
1361 ms |
3576 KB |
Output is correct |
40 |
Correct |
4899 ms |
3752 KB |
Output is correct |
41 |
Correct |
2939 ms |
2552 KB |
Output is correct |
42 |
Correct |
3776 ms |
5624 KB |
Output is correct |
43 |
Correct |
3715 ms |
5628 KB |
Output is correct |
44 |
Correct |
3789 ms |
5240 KB |
Output is correct |
45 |
Correct |
867 ms |
3576 KB |
Output is correct |
46 |
Correct |
1543 ms |
3524 KB |
Output is correct |
47 |
Correct |
1319 ms |
3576 KB |
Output is correct |
48 |
Correct |
927 ms |
3192 KB |
Output is correct |
49 |
Correct |
1431 ms |
3576 KB |
Output is correct |
50 |
Correct |
1400 ms |
3576 KB |
Output is correct |
51 |
Correct |
1447 ms |
3576 KB |
Output is correct |
52 |
Correct |
653 ms |
3524 KB |
Output is correct |
53 |
Correct |
662 ms |
3528 KB |
Output is correct |
54 |
Correct |
620 ms |
3576 KB |
Output is correct |
55 |
Correct |
664 ms |
3576 KB |
Output is correct |
56 |
Correct |
667 ms |
3448 KB |
Output is correct |
57 |
Correct |
451 ms |
3320 KB |
Output is correct |
58 |
Correct |
3923 ms |
4600 KB |
Output is correct |
59 |
Correct |
3901 ms |
4628 KB |
Output is correct |
60 |
Correct |
2606 ms |
3856 KB |
Output is correct |
61 |
Correct |
2710 ms |
7416 KB |
Output is correct |
62 |
Correct |
1571 ms |
3704 KB |
Output is correct |
63 |
Correct |
2753 ms |
11192 KB |
Output is correct |