Submission #696503

# Submission time Handle Problem Language Result Execution time Memory
696503 2023-02-06T15:57:58 Z rainboy Sandwich (JOI16_sandwich) C
100 / 100
1293 ms 15032 KB
/* 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 ", ans[i][j] == INF ? -1 : ans[i][j]);
		printf("\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 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 420 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 420 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 2 ms 420 KB Output is correct
15 Correct 2 ms 340 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 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 416 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 444 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 3 ms 596 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 420 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 420 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 3 ms 340 KB Output is correct
14 Correct 2 ms 420 KB Output is correct
15 Correct 2 ms 340 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 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 416 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 444 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 3 ms 596 KB Output is correct
28 Correct 2 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 23 ms 2188 KB Output is correct
32 Correct 25 ms 2220 KB Output is correct
33 Correct 8 ms 1424 KB Output is correct
34 Correct 677 ms 2444 KB Output is correct
35 Correct 488 ms 2008 KB Output is correct
36 Correct 14 ms 1412 KB Output is correct
37 Correct 707 ms 2468 KB Output is correct
38 Correct 445 ms 2448 KB Output is correct
39 Correct 632 ms 2336 KB Output is correct
40 Correct 1269 ms 2716 KB Output is correct
41 Correct 456 ms 1792 KB Output is correct
42 Correct 1043 ms 2848 KB Output is correct
43 Correct 1030 ms 3016 KB Output is correct
44 Correct 1070 ms 2772 KB Output is correct
45 Correct 655 ms 2432 KB Output is correct
46 Correct 432 ms 2460 KB Output is correct
47 Correct 620 ms 2448 KB Output is correct
48 Correct 338 ms 2120 KB Output is correct
49 Correct 475 ms 2448 KB Output is correct
50 Correct 541 ms 2448 KB Output is correct
51 Correct 441 ms 2572 KB Output is correct
52 Correct 452 ms 2436 KB Output is correct
53 Correct 451 ms 2508 KB Output is correct
54 Correct 422 ms 2308 KB Output is correct
55 Correct 439 ms 2440 KB Output is correct
56 Correct 432 ms 2444 KB Output is correct
57 Correct 349 ms 2144 KB Output is correct
58 Correct 986 ms 2688 KB Output is correct
59 Correct 1025 ms 2820 KB Output is correct
60 Correct 690 ms 2244 KB Output is correct
61 Correct 1045 ms 2780 KB Output is correct
62 Correct 1293 ms 15024 KB Output is correct
63 Correct 1122 ms 15032 KB Output is correct