#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast","unroll-loops")
//#pragma GCC target("popcnt")
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 400 + 7;
int n, m;
int vis[N][N];
int il = 0;
string s[N];
int answer[N][N];
queue<int> q;
inline void Clean()
{
il = 0;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
vis[i][j] = 0;
}
bool DFS(int r, int i, int j)
{
if(vis[i][j])
return vis[i][j] == 2;
vis[i][j] = 2;
++il; ++il;
if(r == 0)
{
if(i > 1 && DFS(0, i - 1, j)) return true;
if(j > 1 && s[i][j] == 'Z')
if(DFS((s[i][j - 1] != 'Z'), i, j - 1))
return true;
if(j < m && s[i][j] == 'N' && DFS((s[i][j + 1] != 'N'), i, j + 1))
return true;
}else
{
if(i < n && DFS(1, i + 1, j)) return true;
if(j > 1 && s[i][j] == 'N' && DFS((s[i][j - 1] != 'Z'), i, j - 1))
return true;
if(j < m && s[i][j] == 'Z' && DFS((s[i][j + 1] != 'N'), i, j + 1))
return true;
}
vis[i][j] = 1;
return false;
}
void Solve()
{
cin >> n >> m;
for(int i = 1; i <= n; ++i)
{
cin >> s[i];
s[i] = '#' + s[i];
for(int j = 1; j <= m; ++j)
answer[i][j] = N * N * 4;
}
for(int j = 1; j <= m; ++j)
{
for(int i = 1; i <= n; ++i)
{
if(DFS(0, i, j))
break;
answer[i][j] = min(answer[i][j], il);
}
Clean();
for(int i = n; i >= 1; --i)
{
if(DFS(1, i, j))
break;
answer[i][j] = min(answer[i][j], il);
}
Clean();
}
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
{
if(answer[i][j] == N * N * 4) answer[i][j] = -1;
cout << answer[i][j] << " ";
}
cout << "\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//int t; cin >> t;
//while(t--)
Solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
484 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
656 KB |
Output is correct |
26 |
Correct |
2 ms |
600 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
484 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
656 KB |
Output is correct |
26 |
Correct |
2 ms |
600 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
1628 KB |
Output is correct |
31 |
Correct |
24 ms |
2716 KB |
Output is correct |
32 |
Correct |
25 ms |
2656 KB |
Output is correct |
33 |
Correct |
6 ms |
1640 KB |
Output is correct |
34 |
Correct |
386 ms |
2876 KB |
Output is correct |
35 |
Correct |
279 ms |
2400 KB |
Output is correct |
36 |
Correct |
2 ms |
1628 KB |
Output is correct |
37 |
Correct |
458 ms |
2876 KB |
Output is correct |
38 |
Correct |
258 ms |
2900 KB |
Output is correct |
39 |
Correct |
435 ms |
2896 KB |
Output is correct |
40 |
Correct |
891 ms |
3152 KB |
Output is correct |
41 |
Correct |
579 ms |
2132 KB |
Output is correct |
42 |
Correct |
764 ms |
3236 KB |
Output is correct |
43 |
Correct |
745 ms |
3156 KB |
Output is correct |
44 |
Correct |
732 ms |
3152 KB |
Output is correct |
45 |
Correct |
353 ms |
2856 KB |
Output is correct |
46 |
Correct |
252 ms |
2900 KB |
Output is correct |
47 |
Correct |
274 ms |
2852 KB |
Output is correct |
48 |
Correct |
156 ms |
2640 KB |
Output is correct |
49 |
Correct |
241 ms |
2900 KB |
Output is correct |
50 |
Correct |
269 ms |
2860 KB |
Output is correct |
51 |
Correct |
248 ms |
2896 KB |
Output is correct |
52 |
Correct |
232 ms |
2904 KB |
Output is correct |
53 |
Correct |
233 ms |
2896 KB |
Output is correct |
54 |
Correct |
228 ms |
2896 KB |
Output is correct |
55 |
Correct |
250 ms |
2896 KB |
Output is correct |
56 |
Correct |
245 ms |
2900 KB |
Output is correct |
57 |
Correct |
167 ms |
2512 KB |
Output is correct |
58 |
Correct |
654 ms |
3184 KB |
Output is correct |
59 |
Correct |
677 ms |
3172 KB |
Output is correct |
60 |
Correct |
447 ms |
2692 KB |
Output is correct |
61 |
Correct |
664 ms |
3144 KB |
Output is correct |
62 |
Correct |
649 ms |
2840 KB |
Output is correct |
63 |
Correct |
624 ms |
3156 KB |
Output is correct |