結果

問題 No.1572 XI
ユーザー 👑 ygussanyygussany
提出日時 2021-06-27 13:54:12
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 191 ms / 2,000 ms
コード長 2,302 bytes
コンパイル時間 206 ms
コンパイル使用メモリ 30,620 KB
実行使用メモリ 73,396 KB
最終ジャッジ日時 2023-09-07 17:42:50
合計ジャッジ時間 5,863 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
14,388 KB
testcase_01 AC 4 ms
13,076 KB
testcase_02 AC 4 ms
13,012 KB
testcase_03 AC 4 ms
12,972 KB
testcase_04 AC 44 ms
35,516 KB
testcase_05 AC 94 ms
47,280 KB
testcase_06 AC 113 ms
52,368 KB
testcase_07 AC 6 ms
13,632 KB
testcase_08 AC 98 ms
49,784 KB
testcase_09 AC 49 ms
38,496 KB
testcase_10 AC 76 ms
44,432 KB
testcase_11 AC 9 ms
14,600 KB
testcase_12 AC 36 ms
33,932 KB
testcase_13 AC 13 ms
22,544 KB
testcase_14 AC 37 ms
32,916 KB
testcase_15 AC 10 ms
15,040 KB
testcase_16 AC 31 ms
33,328 KB
testcase_17 AC 8 ms
26,240 KB
testcase_18 AC 42 ms
35,824 KB
testcase_19 AC 99 ms
50,116 KB
testcase_20 AC 66 ms
42,288 KB
testcase_21 AC 115 ms
54,052 KB
testcase_22 AC 90 ms
47,816 KB
testcase_23 AC 6 ms
14,260 KB
testcase_24 AC 4 ms
13,088 KB
testcase_25 AC 3 ms
14,224 KB
testcase_26 AC 3 ms
12,988 KB
testcase_27 AC 4 ms
12,944 KB
testcase_28 AC 3 ms
13,196 KB
testcase_29 AC 4 ms
13,184 KB
testcase_30 AC 3 ms
13,212 KB
testcase_31 AC 3 ms
12,960 KB
testcase_32 AC 3 ms
14,384 KB
testcase_33 AC 4 ms
14,436 KB
testcase_34 AC 175 ms
69,132 KB
testcase_35 AC 170 ms
67,236 KB
testcase_36 AC 173 ms
69,176 KB
testcase_37 AC 184 ms
71,408 KB
testcase_38 AC 173 ms
69,052 KB
testcase_39 AC 182 ms
69,260 KB
testcase_40 AC 166 ms
67,044 KB
testcase_41 AC 178 ms
69,232 KB
testcase_42 AC 178 ms
69,288 KB
testcase_43 AC 174 ms
69,368 KB
testcase_44 AC 191 ms
73,272 KB
testcase_45 AC 191 ms
73,396 KB
testcase_46 AC 189 ms
73,336 KB
testcase_47 AC 15 ms
26,248 KB
testcase_48 AC 182 ms
73,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

int q[6000000][3];

int main()
{
	int i, H, W, s[2], g[2];
	char A[1002][1002] = {};
	scanf("%d %d", &H, &W);
	scanf("%d %d", &(s[0]), &(s[1]));
	scanf("%d %d", &(g[0]), &(g[1]));
	for (i = 1; i <= H; i++) scanf("%s", &(A[i][1]));
	
	const int sup = 1 << 30;
	int j, k, l, dist[6][1001][1001], head, tail;
	for (k = 0; k < 6; k++) for (i = 1; i <= H; i++) for (j = 1; j <= W; j++) dist[k][i][j] = sup;
	dist[0][s[0]][s[1]] = 0;
	q[0][0] = 0;
	q[0][1] = s[0];
	q[0][2] = s[1];
	for (head = 0, tail = 1; head < tail; head++) {
		k = q[head][0];
		i = q[head][1];
		j = q[head][2];
		
		if (A[i-1][j] == '.') {
			switch (k) {
			case 0:
				l = 1;
				break;
			case 1:
				l = 5;
				break;
			case 2:
				l = 2;
				break;
			case 3:
				l = 0;
				break;
			case 4:
				l = 4;
				break;
			case 5:
				l = 3;
				break;
			}
			if (dist[l][i-1][j] == sup) {
				dist[l][i-1][j] = dist[k][i][j] + 1;
				q[tail][0] = l;
				q[tail][1] = i - 1;
				q[tail++][2] = j;
			}
		}
		if (A[i+1][j] == '.') {
			switch (k) {
			case 0:
				l = 3;
				break;
			case 1:
				l = 0;
				break;
			case 2:
				l = 2;
				break;
			case 3:
				l = 5;
				break;
			case 4:
				l = 4;
				break;
			case 5:
				l = 1;
				break;
			}
			if (dist[l][i+1][j] == sup) {
				dist[l][i+1][j] = dist[k][i][j] + 1;
				q[tail][0] = l;
				q[tail][1] = i + 1;
				q[tail++][2] = j;
			}
		}
		if (A[i][j-1] == '.') {
			switch (k) {
			case 0:
				l = 4;
				break;
			case 1:
				l = 1;
				break;
			case 2:
				l = 0;
				break;
			case 3:
				l = 3;
				break;
			case 4:
				l = 5;
				break;
			case 5:
				l = 2;
				break;
			}
			if (dist[l][i][j-1] == sup) {
				dist[l][i][j-1] = dist[k][i][j] + 1;
				q[tail][0] = l;
				q[tail][1] = i;
				q[tail++][2] = j - 1;
			}
		}
		if (A[i][j+1] == '.') {
			switch (k) {
			case 0:
				l = 2;
				break;
			case 1:
				l = 1;
				break;
			case 2:
				l = 5;
				break;
			case 3:
				l = 3;
				break;
			case 4:
				l = 0;
				break;
			case 5:
				l = 4;
				break;
			}
			if (dist[l][i][j+1] == sup) {
				dist[l][i][j+1] = dist[k][i][j] + 1;
				q[tail][0] = l;
				q[tail][1] = i;
				q[tail++][2] = j + 1;
			}
		}
	}
	if (dist[0][g[0]][g[1]] == sup) printf("-1\n");
	else printf("%d\n", dist[0][g[0]][g[1]]);
	fflush(stdout);
	return 0;
}
0