/* https://www.ioi-jp.org/camp/2016/2016-sp-tasks/2016-sp-d2-sandwich-review.pdf */
#include <stdio.h>
#include <string.h>
#define N 400
#define M 400
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
char cc[N][M + 1], visited[N][M], tt[N][M]; int n, m, cnt;
int dfs(int i, int j, int t) {
if (visited[i][j])
return visited[i][j] == 2 || tt[i][j] != t;
visited[i][j] = 2, tt[i][j] = t, cnt++;
if (t == 0) {
if (j + 1 < m && dfs(i, j + 1, 0))
return 1;
if (cc[i][j] == 'N' && i > 0 && dfs(i - 1, j, cc[i - 1][j] == 'N' ? 0 : 1))
return 1;
if (cc[i][j] == 'Z' && i + 1 < n && dfs(i + 1, j, cc[i + 1][j] == 'Z' ? 0 : 1))
return 1;
} else {
if (j > 0 && dfs(i, j - 1, 1))
return 1;
if (cc[i][j] == 'Z' && i > 0 && dfs(i - 1, j, cc[i - 1][j] == 'N' ? 0 : 1))
return 1;
if (cc[i][j] == 'N' && i + 1 < n && dfs(i + 1, j, cc[i + 1][j] == 'Z' ? 0 : 1))
return 1;
}
visited[i][j] = 1;
return 0;
}
int main() {
static int ans[N][M];
int i, i_, j, j1, j2, r, tmp;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%s", cc[i]);
for (i = 0; i < n; i++)
memset(ans[i], 0x3f, m * sizeof *ans[i]);
for (r = 0; r < 2; r++) {
for (i = 0; i < n; i++) {
for (i_ = 0; i_ < n; i_++)
memset(visited[i_], 0, m * sizeof *visited[i_]);
cnt = 0;
for (j = 0; j < m; j++) {
if (dfs(i, j, 1))
break;
ans[i][j] = min(ans[i][j], cnt * 2);
}
}
for (i = 0; i < n; i++) {
for (j1 = 0, j2 = m - 1; j1 < j2; j1++, j2--) {
tmp = cc[i][j1], cc[i][j1] = cc[i][j2], cc[i][j2] = tmp;
tmp = ans[i][j1], ans[i][j1] = ans[i][j2], ans[i][j2] = tmp;
}
for (j = 0; j < m; j++)
cc[i][j] = cc[i][j] == 'N' ? 'Z' : 'N';
}
}
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
printf("%d%c", ans[i][j] == INF ? -1 : ans[i][j], j + 1 < m ? ' ' : '\n');
return 0;
}
Compilation message
sandwich.c: In function 'main':
sandwich.c:40:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
sandwich.c:42:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%s", cc[i]);
| ^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
432 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
372 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
448 KB |
Output is correct |
15 |
Correct |
2 ms |
352 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
432 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
372 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
448 KB |
Output is correct |
15 |
Correct |
2 ms |
352 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
596 KB |
Output is correct |
28 |
Correct |
3 ms |
596 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
4 ms |
1364 KB |
Output is correct |
31 |
Correct |
24 ms |
2080 KB |
Output is correct |
32 |
Correct |
25 ms |
2124 KB |
Output is correct |
33 |
Correct |
7 ms |
1364 KB |
Output is correct |
34 |
Correct |
696 ms |
2284 KB |
Output is correct |
35 |
Correct |
461 ms |
1888 KB |
Output is correct |
36 |
Correct |
14 ms |
1364 KB |
Output is correct |
37 |
Correct |
698 ms |
2300 KB |
Output is correct |
38 |
Correct |
444 ms |
2328 KB |
Output is correct |
39 |
Correct |
649 ms |
2176 KB |
Output is correct |
40 |
Correct |
1303 ms |
2500 KB |
Output is correct |
41 |
Correct |
464 ms |
1696 KB |
Output is correct |
42 |
Correct |
1096 ms |
2680 KB |
Output is correct |
43 |
Correct |
1037 ms |
2752 KB |
Output is correct |
44 |
Correct |
1064 ms |
2612 KB |
Output is correct |
45 |
Correct |
656 ms |
2280 KB |
Output is correct |
46 |
Correct |
448 ms |
2276 KB |
Output is correct |
47 |
Correct |
599 ms |
2300 KB |
Output is correct |
48 |
Correct |
337 ms |
1988 KB |
Output is correct |
49 |
Correct |
459 ms |
2296 KB |
Output is correct |
50 |
Correct |
528 ms |
2288 KB |
Output is correct |
51 |
Correct |
442 ms |
2284 KB |
Output is correct |
52 |
Correct |
433 ms |
2284 KB |
Output is correct |
53 |
Correct |
443 ms |
2280 KB |
Output is correct |
54 |
Correct |
423 ms |
2152 KB |
Output is correct |
55 |
Correct |
444 ms |
2288 KB |
Output is correct |
56 |
Correct |
436 ms |
2276 KB |
Output is correct |
57 |
Correct |
350 ms |
2016 KB |
Output is correct |
58 |
Correct |
999 ms |
2520 KB |
Output is correct |
59 |
Correct |
1010 ms |
2648 KB |
Output is correct |
60 |
Correct |
693 ms |
2120 KB |
Output is correct |
61 |
Correct |
1057 ms |
2716 KB |
Output is correct |
62 |
Correct |
1331 ms |
14744 KB |
Output is correct |
63 |
Correct |
1126 ms |
14864 KB |
Output is correct |