답안 #471552

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
471552 2021-09-09T17:57:20 Z rainboy Portal (COCI17_portal) C
120 / 120
153 ms 32416 KB
/* Source: BOI 2014 day 1, "Portals" */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	500
#define M	500
#define N_	(N * M)
#define INF	0x3f3f3f3f

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

int *oj[N_], *ow[N_], oo[N_];

void link(int i, int j, int w) {
	int o = oo[i]++;

	if (o >= 2 && (o & o - 1) == 0) {
		oj[i] = (int *) realloc(oj[i], o * 2 * sizeof *oj[i]);
		ow[i] = (int *) realloc(ow[i], o * 2 * sizeof *ow[i]);
	}
	oj[i][o] = j, ow[i][o] = w;
}

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);
	pq[i] = 0;
	return i;
}

int dijkstra(int n, int s, int t) {
	memset(dd, 0x3f, n * sizeof *dd);
	dd[s] = 0, pq_add_last(s);
	while (cnt) {
		int i = pq_remove_first(), o;

		if (i == t)
			return dd[t];
		for (o = 0; o < oo[i]; o++) {
			int j = oj[i][o], d = dd[i] + ow[i][o];

			if (dd[j] > d) {
				if (dd[j] == INF)
					pq_add_last(j);
				dd[j] = d, pq_up(j);
			}
		}
	}
	return INF;
}

int main() {
	static char cc[N][M + 1];
	static int dd[N][M], qu[N * M];
	int n, m, i, j, is, js, ic, jc, head, cnt, ans;

	scanf("%d%d", &n, &m), n -= 2, m -= 2;
	for (i = -1; i <= n; i++) {
		static char s[M + 1];

		scanf("%s", s);
		if (i >= 0 && i < n)
			memcpy(cc[i], s + 1, m * sizeof *s);
	}
	head = cnt = 0;
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			if (i == 0 || i == n - 1 || j == 0 || j == m - 1 || cc[i][j - 1] == '#' || cc[i][j + 1] == '#' || cc[i - 1][j] == '#' || cc[i + 1][j] == '#')
				dd[i][j] = 0, qu[head + cnt++] = i * m + j;
			else
				dd[i][j] = n * m;
	while (cnt) {
		int ij, d, h;

		ij = qu[cnt--, head++], i = ij / m, j = ij % m, d = dd[i][j] + 1;
		for (h = 0; h < 4; h++) {
			int i_ = i + di[h], j_ = j + dj[h];

			if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m && dd[i_][j_] > d)
				dd[i_][j_] = d, qu[head + cnt++] = i_ * m + j_;
		}
	}
	for (i = 0; i < n * m; i++) {
		oj[i] = (int *) malloc(2 * sizeof *oj[i]);
		ow[i] = (int *) malloc(2 * sizeof *ow[i]);
	}
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			if (cc[i][j] != '#') {
				if (i > 0 && cc[i - 1][j] != '#') {
					link(i * m + j, (i - 1) * m + j, 1);
					link((i - 1) * m + j, i * m + j, 1);
				}
				if (j > 0 && cc[i][j - 1] != '#') {
					link(i * m + j, i * m + j - 1, 1);
					link(i * m + j - 1, i * m + j, 1);
				}
			}
	for (i = 0; i < n; i++) {
		int j_;

		for (j = 0, j_ = 0; j < m; j++)
			if (cc[i][j] == '#')
				j_ = j + 1;
			else
				link(i * m + j, i * m + j_, dd[i][j] + 1);
		for (j = m - 1, j_ = m - 1; j >= 0; j--)
			if (cc[i][j] == '#')
				j_ = j - 1;
			else
				link(i * m + j, i * m + j_, dd[i][j] + 1);
	}
	for (j = 0; j < m; j++) {
		int i_;

		for (i = 0, i_ = 0; i < n; i++)
			if (cc[i][j] == '#')
				i_ = i + 1;
			else
				link(i * m + j, i_ * m + j, dd[i][j] + 1);
		for (i = n - 1, i_ = n - 1; i >= 0; i--)
			if (cc[i][j] == '#')
				i_ = i - 1;
			else
				link(i * m + j, i_ * m + j, dd[i][j] + 1);
	}
	is = js = -1, ic = jc = -1;
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			if (cc[i][j] == 'C')
				is = i, js = j;
			else if (cc[i][j] == 'F')
				ic = i, jc = j;
	ans = dijkstra(n * m, is * m + js, ic * m + jc);
	if (ans == INF)
		printf("nemoguce\n");
	else
		printf("%d\n", ans);
	return 0;
}

Compilation message

portal.c: In function 'link':
portal.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   19 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
portal.c: In function 'main':
portal.c:89:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |  scanf("%d%d", &n, &m), n -= 2, m -= 2;
      |  ^~~~~~~~~~~~~~~~~~~~~
portal.c:93:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |   scanf("%s", s);
      |   ^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 284 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3020 KB Output is correct
2 Correct 7 ms 2764 KB Output is correct
3 Correct 11 ms 3500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 7916 KB Output is correct
2 Correct 12 ms 5708 KB Output is correct
3 Correct 16 ms 4940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 19864 KB Output is correct
2 Correct 44 ms 14244 KB Output is correct
3 Correct 46 ms 12000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 27904 KB Output is correct
2 Correct 70 ms 24680 KB Output is correct
3 Correct 83 ms 19248 KB Output is correct
4 Correct 56 ms 15880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 153 ms 32416 KB Output is correct
2 Correct 96 ms 28996 KB Output is correct
3 Correct 116 ms 31300 KB Output is correct
4 Correct 115 ms 31268 KB Output is correct