#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MN = 444;
int N, M;
char G[MN][MN];
int vis[MN][MN];
int ans[MN][MN];
int cnt;
void dfs(int r, int c, int t) {
if(vis[r][c] == 2) return;
if(vis[r][c] == 1) {
cnt = 1e9;
return;
}
vis[r][c] = 1;
cnt++;
if(t) {
if(G[r][c] == 'N') {
if(r) dfs(r - 1, c, 1);
if(c + 1 < M) dfs(r, c + 1, G[r][c + 1] == 'N');
}
else {
if(r) dfs(r - 1, c, 1);
if(c) dfs(r, c - 1, G[r][c - 1] == 'Z');
}
}
else {
if(G[r][c] == 'N') {
if(r + 1 < N) dfs(r + 1, c, 0);
if(c) dfs(r, c - 1, G[r][c - 1] == 'Z');
}
else {
if(r + 1 < N) dfs(r + 1, c, 0);
if(c + 1 < M) dfs(r, c + 1, G[r][c + 1] == 'N');
}
}
vis[r][c] = 2;
}
int main() {
scanf("%d %d", &N, &M);
for(int i = 0; i < N; i++) {
scanf("\n");
for(int j = 0; j < M; j++) {
scanf("%c", &G[i][j]);
}
}
for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) ans[i][j] = 1e9;
for(int i = 0; i < M; i++) {
memset(vis, 0, sizeof(vis));
cnt = 0;
for(int j = 0; j < N; j++) {
if(!vis[j][i]) dfs(j, i, 1);
ans[j][i] = min(ans[j][i], cnt);
}
memset(vis, 0, sizeof(vis));
cnt = 0;
for(int j = N - 1; j >= 0; j--) {
if(!vis[j][i]) dfs(j, i, 0);
ans[j][i] = min(ans[j][i], cnt);
}
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
if(ans[i][j] >= 1e9) printf("-1 ");
else printf("%d ", 2 * ans[i][j]);
}
printf("\n");
}
}
Compilation message
sandwich.cpp: In function 'int main()':
sandwich.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
sandwich.cpp:52:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("\n");
~~~~~^~~~~~
sandwich.cpp:54:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%c", &G[i][j]);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
7 ms |
1144 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1144 KB |
Output is correct |
6 |
Correct |
12 ms |
1272 KB |
Output is correct |
7 |
Correct |
9 ms |
1272 KB |
Output is correct |
8 |
Correct |
10 ms |
1272 KB |
Output is correct |
9 |
Correct |
8 ms |
1148 KB |
Output is correct |
10 |
Correct |
10 ms |
1272 KB |
Output is correct |
11 |
Correct |
10 ms |
1272 KB |
Output is correct |
12 |
Correct |
8 ms |
1144 KB |
Output is correct |
13 |
Correct |
10 ms |
1272 KB |
Output is correct |
14 |
Correct |
10 ms |
1272 KB |
Output is correct |
15 |
Correct |
8 ms |
1144 KB |
Output is correct |
16 |
Correct |
8 ms |
1144 KB |
Output is correct |
17 |
Correct |
8 ms |
1272 KB |
Output is correct |
18 |
Correct |
8 ms |
1272 KB |
Output is correct |
19 |
Correct |
9 ms |
1144 KB |
Output is correct |
20 |
Correct |
9 ms |
1320 KB |
Output is correct |
21 |
Correct |
9 ms |
1144 KB |
Output is correct |
22 |
Correct |
8 ms |
1144 KB |
Output is correct |
23 |
Correct |
8 ms |
1144 KB |
Output is correct |
24 |
Correct |
10 ms |
1244 KB |
Output is correct |
25 |
Correct |
10 ms |
1260 KB |
Output is correct |
26 |
Correct |
11 ms |
1304 KB |
Output is correct |
27 |
Correct |
10 ms |
1276 KB |
Output is correct |
28 |
Correct |
10 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
7 ms |
1144 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1144 KB |
Output is correct |
6 |
Correct |
12 ms |
1272 KB |
Output is correct |
7 |
Correct |
9 ms |
1272 KB |
Output is correct |
8 |
Correct |
10 ms |
1272 KB |
Output is correct |
9 |
Correct |
8 ms |
1148 KB |
Output is correct |
10 |
Correct |
10 ms |
1272 KB |
Output is correct |
11 |
Correct |
10 ms |
1272 KB |
Output is correct |
12 |
Correct |
8 ms |
1144 KB |
Output is correct |
13 |
Correct |
10 ms |
1272 KB |
Output is correct |
14 |
Correct |
10 ms |
1272 KB |
Output is correct |
15 |
Correct |
8 ms |
1144 KB |
Output is correct |
16 |
Correct |
8 ms |
1144 KB |
Output is correct |
17 |
Correct |
8 ms |
1272 KB |
Output is correct |
18 |
Correct |
8 ms |
1272 KB |
Output is correct |
19 |
Correct |
9 ms |
1144 KB |
Output is correct |
20 |
Correct |
9 ms |
1320 KB |
Output is correct |
21 |
Correct |
9 ms |
1144 KB |
Output is correct |
22 |
Correct |
8 ms |
1144 KB |
Output is correct |
23 |
Correct |
8 ms |
1144 KB |
Output is correct |
24 |
Correct |
10 ms |
1244 KB |
Output is correct |
25 |
Correct |
10 ms |
1260 KB |
Output is correct |
26 |
Correct |
11 ms |
1304 KB |
Output is correct |
27 |
Correct |
10 ms |
1276 KB |
Output is correct |
28 |
Correct |
10 ms |
1272 KB |
Output is correct |
29 |
Correct |
36 ms |
1196 KB |
Output is correct |
30 |
Correct |
5 ms |
2040 KB |
Output is correct |
31 |
Correct |
3679 ms |
6520 KB |
Output is correct |
32 |
Correct |
3674 ms |
6392 KB |
Output is correct |
33 |
Correct |
292 ms |
3064 KB |
Output is correct |
34 |
Correct |
903 ms |
3192 KB |
Output is correct |
35 |
Correct |
848 ms |
2808 KB |
Output is correct |
36 |
Correct |
5 ms |
2040 KB |
Output is correct |
37 |
Correct |
1194 ms |
3164 KB |
Output is correct |
38 |
Correct |
1158 ms |
3192 KB |
Output is correct |
39 |
Correct |
1176 ms |
3192 KB |
Output is correct |
40 |
Correct |
1663 ms |
3292 KB |
Output is correct |
41 |
Correct |
1045 ms |
2428 KB |
Output is correct |
42 |
Correct |
1614 ms |
3704 KB |
Output is correct |
43 |
Correct |
1607 ms |
3448 KB |
Output is correct |
44 |
Correct |
1604 ms |
3576 KB |
Output is correct |
45 |
Correct |
881 ms |
3080 KB |
Output is correct |
46 |
Correct |
615 ms |
3192 KB |
Output is correct |
47 |
Correct |
691 ms |
3088 KB |
Output is correct |
48 |
Correct |
409 ms |
2808 KB |
Output is correct |
49 |
Correct |
616 ms |
3192 KB |
Output is correct |
50 |
Correct |
748 ms |
3192 KB |
Output is correct |
51 |
Correct |
620 ms |
3192 KB |
Output is correct |
52 |
Correct |
661 ms |
3192 KB |
Output is correct |
53 |
Correct |
636 ms |
3064 KB |
Output is correct |
54 |
Correct |
593 ms |
3068 KB |
Output is correct |
55 |
Correct |
626 ms |
3192 KB |
Output is correct |
56 |
Correct |
642 ms |
3068 KB |
Output is correct |
57 |
Correct |
440 ms |
2780 KB |
Output is correct |
58 |
Correct |
1636 ms |
3936 KB |
Output is correct |
59 |
Correct |
1678 ms |
3724 KB |
Output is correct |
60 |
Correct |
1172 ms |
3324 KB |
Output is correct |
61 |
Correct |
1940 ms |
7032 KB |
Output is correct |
62 |
Correct |
1515 ms |
3320 KB |
Output is correct |
63 |
Correct |
1518 ms |
3192 KB |
Output is correct |