# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
544763 | rainboy | Connect (CEOI06_connect) | C11 | 36 ms | 36712 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |