답안 #375028

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
375028 2021-03-08T21:21:53 Z rainboy 로봇 (APIO13_robots) C
0 / 100
1 ms 364 KB
#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define N	500
#define M	500
#define K	9
#define N_	((N + M) * 2 * K * K)
#define INF	0x3f3f3f3f

int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };

char cc[N][M + 1]; int dp[N][M][K], n, m, k;

int solve(int i, int j, int h) {
	if (dp[i][j][h] == -1) {
		int h_, i_, j_;

		dp[i][j][h] = -2;
		if (cc[i][j] == 'C')
			h_ = (h + 1) % 4;
		else if (cc[i][j] == 'A')
			h_ = (h + 3) % 4;
		else
			h_ = h;
		i_ = i + di[h_], j_ = j + dj[h_];
		if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m)
			dp[i][j][h] = solve(i_, j_, h_);
		else
			dp[i][j][h] = i * m + j;
	}
	return dp[i][j][h];
}

int iq[1 + N_], pq[N_], dd[N_], cnt;

int lt(int i, int j) {
	return dd[i] < dd[j];
}

int p2(int p) {
	return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}

void pq_up(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_dn(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_add_last(int i) {
	iq[pq[i] = ++cnt] = i;
}

int pq_remove_first() {
	int i = iq[1], j = iq[cnt--];

	if (j != i)
		pq[j] = 1, pq_dn(j);
	return i;
}

void b2ij(int b, int *i, int *j) {
	if (b < m)
		*i = 0, *j = b;
	else if ((b -= m) < m)
		*i = n - 1, *j = b;
	else if ((b -= m) < n)
		*i = b, *j = 0;
	else
		*i = 0, *j = b - n;
}

int ij2b(int i, int j) {
	if (i == 0)
		return j;
	else if (i == n - 1)
		return m + j;
	else if (j == 0)
		return m + m + i;
	else
		return m + m + n + i;
}

int main() {
	int k, h, i, j, l, r;

	scanf("%d%d%d", &k, &m, &n);
	for (i = 0; i < n; i++)
		scanf("%s", cc[i]);
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			for (h = 0; h < 4; h++)
				dp[i][j][h] = -1;
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			for (h = 0; h < 4; h++)
				solve(i, j, h);
	memset(dd, 0x3f, ((n + m) * 2 * k * k) * sizeof *dd);
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			if (isdigit(cc[i][j])) {
				int a, b, d, u;

				a = cc[i][j] - '1';
				if (i == 0 || i == n - 1 || j == 0 || j == m - 1) {
					u = (ij2b(i, j) * k + a) * k + a, d = 0;
					if (dd[u] > d) {
						if (dd[u] == INF)
							pq_add_last(u);
						dd[u] = d, pq_up(u);
					}
				} else
					for (h = 0; h < 4; h++) {
						b = ij2b(dp[i][j][h] / m, dp[i][j][h] % m), u = (b * k + a) * k + a, d = 1;
						if (dd[u] > d) {
							if (dd[u] == INF)
								pq_add_last(u);
							dd[u] = d, pq_up(u);
						}
					}
			}
	while (cnt) {
		int u = pq_remove_first(), b, v, d, l_, r_;

		l = u / k % k, r = u % k, b2ij(b = u / k / k, &i, &j);
		if (l == 0 && r == k - 1) {
			printf("%d\n", dd[u]);
			return 0;
		}
		if (u >= k) {
			for (r_ = r + 1; r_ < k; r_++) {
				v = (b * k + l) * k + r_, d = dd[u] + dd[(b * k + r + 1) * k + r_];
				if (dd[v] > d) {
					if (dd[v] == INF)
						pq_add_last(v);
					dd[v] = d, pq_up(v);
				}
			}
			for (l_ = l - 1; l_ >= 0; l_--) {
				v = (b * k + l_) * k + r, d = dd[(b * k + l_) * k + l - 1] + dd[u];
				if (dd[v] > d) {
					if (dd[v] == INF)
						pq_add_last(v);
					dd[v] = d, pq_up(v);
				}
			}
		}
		for (h = 0; h < 4; h++)
			if (dp[i][j][h] != -2) {
				b = ij2b(dp[i][j][h] / m, dp[i][j][h] % m), v = (b * k + l) * k + r, d = dd[u] + 1;
				if (dd[v] > d) {
					if (dd[v] == INF)
						pq_add_last(v);
					dd[v] = d, pq_up(v);
				}
			}
	}
	printf("-1\n");
	return 0;
}

Compilation message

robots.c: In function 'main':
robots.c:99:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   99 |  scanf("%d%d%d", &k, &m, &n);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
robots.c:101:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
  101 |   scanf("%s", cc[i]);
      |   ^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 0 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 0 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 0 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 0 ms 364 KB Output isn't correct
3 Halted 0 ms 0 KB -