Submission #544763

# Submission time Handle Problem Language Result Execution time Memory
544763 2022-04-02T16:34:26 Z rainboy Connect (CEOI06_connect) C
100 / 100
36 ms 36712 KB
#include <stdio.h>
#include <string.h>

#define N	12
#define M	39
#define INF	0x3f3f3f3f

int main() {
	static char s[32], aa[N * 2 + 1][M * 2 + 3];
	static int dp[M + 1][N][1 << N][2], bb[M + 1][N][1 << N][2];
	static char cc[M + 1][N][1 << N][2], dd[M + 1][N][1 << N][2];
	int n, m, i, j, b, c;

	fgets(s, 32, stdin), sscanf(s, "%d%d", &n, &m), n /= 2, m /= 2;
	for (i = 0; i <= n * 2; i++)
		fgets(aa[i], m * 2 + 3, stdin);
	memset(dp, 0x3f, sizeof dp);
	dp[0][0][0][0] = 0;
	for (j = 0; j < m; j++)
		for (i = 0; i < n; i++) {
			int (*dp1)[2] = dp[j][i], (*dp2)[2], (*bb2)[2];
			char (*cc2)[2], (*dd2)[2];

			if (i == n - 1)
				dp2 = dp[j + 1][0], bb2 = bb[j + 1][0], cc2 = cc[j + 1][0], dd2 = dd[j + 1][0];
			else
				dp2 = dp[j][i + 1], bb2 = bb[j][i + 1], cc2 = cc[j][i + 1], dd2 = dd[j][i + 1];
			for (b = 0; b < 1 << n; b++)
				for (c = 0; c < 2; c++) {
					int x = dp1[b][c], b_, c_;

					if (x == INF)
						continue;
					if (c == 1) {
						b_ = b & ~(1 << i), c_ = 0;
						if (aa[i * 2 + 1][j * 2 + 1] == ' ')
							if (dp2[b_][c_] > x)
								dp2[b_][c_] = x, bb2[b_][c_] = b, cc2[b_][c_] = c, dd2[b_][c_] = 0;
					} else if (((b & 1 << i) == 0) == (aa[i * 2 + 1][j * 2 + 1] == ' ')) {
						b_ = b & ~(1 << i), c_ = 0;
						if (dp2[b_][c_] > x)
							dp2[b_][c_] = x, bb2[b_][c_] = b, cc2[b_][c_] = c, dd2[b_][c_] = 0;
						if ((b & 1 << i) == 0 && aa[i * 2 + 2][j * 2 + 1] == ' ' && aa[i * 2 + 1][j * 2 + 2] == ' ') {
							b_ = (b | 1 << i) ^ 1 << i + 1, c_ = b >> i + 1 & 1;
							if (dp2[b_][c_] > x + 2)
								dp2[b_][c_] = x + 2, bb2[b_][c_] = b, cc2[b_][c_] = c, dd2[b_][c_] = 3;
						}
					} else {
						if (aa[i * 2 + 2][j * 2 + 1] == ' ') {
							b_ = (b & ~(1 << i)) ^ 1 << i + 1, c_ = b >> i + 1 & 1;
							if (dp2[b_][c_] > x + 1)
								dp2[b_][c_] = x + 1, bb2[b_][c_] = b, cc2[b_][c_] = c, dd2[b_][c_] = 1;
						}
						if (aa[i * 2 + 1][j * 2 + 2] == ' ') {
							b_ = b | 1 << i, c_ = 0;
							if (dp2[b_][c_] > x + 1)
								dp2[b_][c_] = x + 1, bb2[b_][c_] = b, cc2[b_][c_] = c, dd2[b_][c_] = 2;
						}
					}
				}
		}
	j = m, i = 0, b = 0, c = 0;
	while (i > 0 || j > 0) {
		int j_ = i == 0 ? j - 1 : j, i_ = i == 0 ? n - 1 : i - 1, b_ = bb[j][i][b][c], c_ = cc[j][i][b][c];

		if (dd[j][i][b][c] & 1) {
			if (aa[i_ * 2 + 1][j_ * 2 + 1] == ' ')
				aa[i_ * 2 + 1][j_ * 2 + 1] = '.';
			if (aa[i_ * 2 + 2][j_ * 2 + 1] == ' ')
				aa[i_ * 2 + 2][j_ * 2 + 1] = '.';
			if (aa[i_ * 2 + 3][j_ * 2 + 1] == ' ')
				aa[i_ * 2 + 3][j_ * 2 + 1] = '.';
		}
		if (dd[j][i][b][c] & 2) {
			if (aa[i_ * 2 + 1][j_ * 2 + 1] == ' ')
				aa[i_ * 2 + 1][j_ * 2 + 1] = '.';
			if (aa[i_ * 2 + 1][j_ * 2 + 2] == ' ')
				aa[i_ * 2 + 1][j_ * 2 + 2] = '.';
			if (aa[i_ * 2 + 1][j_ * 2 + 3] == ' ')
				aa[i_ * 2 + 1][j_ * 2 + 3] = '.';
		}
		j = j_, i = i_, b = b_, c = c_;
	}
	printf("%d\n", dp[m][0][0][0] * 2);
	for (i = 0; i <= n * 2; i++)
		printf("%s", aa[i]);
	return 0;
}

Compilation message

connect.c: In function 'main':
connect.c:44:35: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   44 |        b_ = (b | 1 << i) ^ 1 << i + 1, c_ = b >> i + 1 & 1;
      |                                 ~~^~~
connect.c:44:52: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   44 |        b_ = (b | 1 << i) ^ 1 << i + 1, c_ = b >> i + 1 & 1;
      |                                                  ~~^~~
connect.c:50:38: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   50 |        b_ = (b & ~(1 << i)) ^ 1 << i + 1, c_ = b >> i + 1 & 1;
      |                                    ~~^~~
connect.c:50:55: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   50 |        b_ = (b & ~(1 << i)) ^ 1 << i + 1, c_ = b >> i + 1 & 1;
      |                                                     ~~^~~
connect.c:14:2: warning: ignoring return value of 'fgets' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |  fgets(s, 32, stdin), sscanf(s, "%d%d", &n, &m), n /= 2, m /= 2;
      |  ^~~~~~~~~~~~~~~~~~~
connect.c:16:3: warning: ignoring return value of 'fgets' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |   fgets(aa[i], m * 2 + 3, stdin);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 16084 KB Output is correct
2 Correct 8 ms 16132 KB Output is correct
3 Correct 7 ms 16468 KB Output is correct
4 Correct 7 ms 16296 KB Output is correct
5 Correct 14 ms 21076 KB Output is correct
6 Correct 7 ms 18032 KB Output is correct
7 Correct 6 ms 16980 KB Output is correct
8 Correct 7 ms 17808 KB Output is correct
9 Correct 8 ms 18220 KB Output is correct
10 Correct 9 ms 19284 KB Output is correct
11 Correct 8 ms 18388 KB Output is correct
12 Correct 10 ms 20796 KB Output is correct
13 Correct 12 ms 20136 KB Output is correct
14 Correct 13 ms 21080 KB Output is correct
15 Correct 14 ms 21716 KB Output is correct
16 Correct 15 ms 20576 KB Output is correct
17 Correct 17 ms 21600 KB Output is correct
18 Correct 32 ms 26236 KB Output is correct
19 Correct 36 ms 36712 KB Output is correct
20 Correct 28 ms 32024 KB Output is correct