답안 #122406

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
122406 2019-06-28T07:15:50 Z 조승현(#2994) 로봇 (APIO13_robots) C++14
100 / 100
195 ms 35552 KB
#include<stdio.h>
#include<algorithm>
using namespace std;
#define INF 999999999
char p[510][510];
int st[10], Next[1000001], N, W, H, ed[1000001], stack[1000001], C, D[45][250001], E[250001][4], Q[1000001], ord[250001], TP[250000], cc, cnt[2500001];
int Num[510][510];
bool v[250001];
int o[4][2] = { {-1,0},{0,1},{1,0},{0,-1} };
int Gap(int a, int b, int c) {
	return ((a - 1)*W + b - 1) * 4 + c;
}
int Gap2(int a, int b) {
	return b - a + (8 - a)*(9 - a) / 2;
}
void Ins(int a, int b, int c) {
	int tc = c;
	if (p[a][b] == 'C')tc = (c + 1) & 3;
	if (p[a][b] == 'A')tc = (c + 3) & 3;
	int x = a + o[tc][0], y = b + o[tc][1];
	if (p[x][y] == 'x') {
		x = Gap(a, b, c);
		Next[x] = -1;
		if (!Num[a][b]) {
			Num[a][b] = ++C;
			ord[C] = x - c;
		}
		ed[x] = Num[a][b];
		return;
	}
	Next[Gap(a, b, c)] = Gap(x, y, tc);
}
void BFS(int a) {
	int h = 0, t = 0, i, x, c = 1;
	for (i = 1; i <= C; i++)v[i] = false;
	Q[++t] = TP[c++];
	v[Q[1]] = true;
	while (h < t) {
		while (v[TP[c]] || (D[a][Q[t]] == D[a][TP[c]] && c <= cc)) {
			if (v[TP[c]]) {
				c++;
				continue;
			}
			Q[++t] = TP[c++];
		}
		x = Q[++h];
		for (i = 0; i < 4; i++) {
			if (E[x][i] && D[a][E[x][i]] > D[a][x] + 1) {
				if (v[E[x][i]]) {
					Q[t] = Q[t];
				}
				D[a][E[x][i]] = D[a][x] + 1;
				Q[++t] = E[x][i];
				v[Q[t]] = true;
			}
		}
		if (h == t) {
			while (c <= cc) {
				if (v[TP[c]]) {
					c++;
					continue;
				}
				Q[++t] = TP[c++];
				break;
			}
		}
	}
	t = t;
}
int main()
{
	int i, j, t, k, t2, l, t3;
	scanf("%d%d%d", &N, &W, &H);
	for (i = 1; i <= H; i++) {
		scanf("%s", p[i] + 1);
		for (j = 1; j <= W; j++) {
			if (p[i][j] >= '1'&&p[i][j] <= '9') {
				st[p[i][j] - '1'] = Gap(i, j, 0);
			}
		}
		p[i][0] = p[i][W + 1] = 'x';
	}
	for (i = 0; i <= W + 1; i++)p[0][i] = p[H + 1][i] = 'x';
	for (i = 1; i <= H; i++) {
		for (j = 1; j <= W; j++) {
			if (p[i][j] == 'x') {
				t = Gap(i, j, 0);
				ed[t] = ed[t + 1] = ed[t + 2] = ed[t + 3] = -1;
				continue;
			}
			Ins(i, j, 0);
			Ins(i, j, 1);
			Ins(i, j, 2);
			Ins(i, j, 3);
		}
	}
	int top = 0;
	for (i = 0; i < W*H * 4; i++) {
		if (!ed[i]) {
			j = i;
			while (!ed[j]) {
				stack[++top] = j;
				j = Next[j];
			}
			j = ed[j];
			while (top)ed[stack[top--]] = j;
		}
	}
	for (i = 0; i < 45; i++)for (j = 1; j <= C; j++)D[i][j] = INF;
	for (i = 1; i <= H; i++) {
		for (j = 1; j <= W; j++) {
			if (!Num[i][j])continue;
			t = Gap(i, j, 0);
			for (k = 0; k < 4; k++) {
				if (ed[t + k] != Num[i][j])E[Num[i][j]][k] = ed[t + k];
			}
		}
	}
	for (i = 0; i < N; i++) {
		t = Gap2(i, i);
		for (j = 0; j < 4; j++) {
			if (ord[ed[st[i] + j]] == st[i])D[t][ed[st[i] + j]] = 0;
			else D[t][ed[st[i] + j]] = min(D[t][ed[st[i] + j]], 1);
			cc = 0;
			TP[++cc] = ed[st[i] + j];
			BFS(t);
		}
	}
	for (i = 1; i < N; i++) {
		for (j = 0; j < N - i; j++) {
			t = Gap2(j, j + i);
			t2 = Gap2(j, j);
			for (k = j; k < j + i; k++) {
				t3 = Gap2(k + 1, j + i);
				for (l = 1; l <= C; l++)D[t][l] = min(D[t][l], D[t2][l] + D[t3][l]);
				t2++;
			}
			t2 = 0;
			for (k = 1; k <= C; k++) {
				if (D[t][k] == INF)continue;
				if (t2 < D[t][k])t2 = D[t][k];
				cnt[D[t][k]]++;
			}
			for (k = 1; k <= t2; k++)cnt[k] += cnt[k - 1];
			cc = cnt[t2];
			for (k = 1; k <= C; k++) {
				if (D[t][k] == INF)continue;
				TP[++cnt[D[t][k] - 1]] = k;
			}
			BFS(t);
			for (k = 0; k <= t2; k++)cnt[k] = 0;
		}
	}
	t = Gap2(0, N - 1);
	t2 = INF;
	for (i = 1; i <= C; i++) {
		if (t2 > D[t][i])t2 = D[t][i];
	}
	if (t2 == INF)t2 = -1;
	printf("%d\n", t2);
}

Compilation message

robots.cpp: In function 'int main()':
robots.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &N, &W, &H);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
robots.cpp:75:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", p[i] + 1);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 684 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 684 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 684 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 36 ms 13132 KB Output is correct
12 Correct 14 ms 12792 KB Output is correct
13 Correct 20 ms 13184 KB Output is correct
14 Correct 50 ms 13244 KB Output is correct
15 Correct 26 ms 12800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 684 KB Output is correct
2 Correct 2 ms 640 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 36 ms 13132 KB Output is correct
12 Correct 14 ms 12792 KB Output is correct
13 Correct 20 ms 13184 KB Output is correct
14 Correct 50 ms 13244 KB Output is correct
15 Correct 26 ms 12800 KB Output is correct
16 Correct 33 ms 11388 KB Output is correct
17 Correct 195 ms 35552 KB Output is correct
18 Correct 70 ms 34552 KB Output is correct
19 Correct 72 ms 34576 KB Output is correct
20 Correct 123 ms 34936 KB Output is correct