結果

問題 No.340 雪の足跡
ユーザー bal4ubal4u
提出日時 2019-07-28 18:03:32
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 267 ms / 1,000 ms
コード長 2,523 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 31,148 KB
実行使用メモリ 10,724 KB
最終ジャッジ日時 2023-09-15 11:33:30
合計ジャッジ時間 4,363 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 3 ms
6,228 KB
testcase_14 AC 17 ms
7,056 KB
testcase_15 AC 18 ms
7,272 KB
testcase_16 AC 10 ms
6,756 KB
testcase_17 AC 21 ms
7,316 KB
testcase_18 AC 20 ms
7,400 KB
testcase_19 AC 21 ms
7,292 KB
testcase_20 AC 57 ms
10,672 KB
testcase_21 AC 15 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 49 ms
10,552 KB
testcase_24 AC 34 ms
10,688 KB
testcase_25 AC 47 ms
9,956 KB
testcase_26 AC 10 ms
7,012 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 15 ms
7,036 KB
testcase_29 AC 162 ms
10,584 KB
testcase_30 AC 115 ms
8,528 KB
testcase_31 AC 240 ms
10,552 KB
testcase_32 AC 267 ms
10,588 KB
testcase_33 AC 255 ms
10,600 KB
testcase_34 AC 266 ms
10,724 KB
testcase_35 AC 236 ms
10,636 KB
testcase_36 AC 236 ms
10,584 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:7:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    7 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:13:24: 備考: in expansion of macro ‘gc’
   13 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: No.340 雪の足跡
// 2019.7.28 bal4u

#include <stdio.h>

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

//// 優先度付きキュー(ダイクストラ法用)
#define MAX 100000
typedef struct { int t, r, c; } QUE;
QUE que[MAX]; int qsize;

#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)

void min_heapify(int i) {
	int l, r, min;
	QUE qt;

	l = LEFT(i), r = RIGHT(i);
	if (l < qsize && que[l].t < que[i].t) min = l; else min = i;
	if (r < qsize && que[r].t < que[min].t) min = r;
	if (min != i) {
		qt = que[i]; que[i] = que[min]; que[min] = qt;
		min_heapify(min);
	}
}

void deq() {
	que[0] = que[--qsize];
	min_heapify(0);
}

void enq(int r, int c, int t) {
	int i, min;
	QUE qt;

	i = qsize++;
	que[i].t = t, que[i].r = r, que[i].c = c;
	while (i > 0 && que[min = PARENT(i)].t > que[i].t) {
		qt = que[i]; que[i] = que[min]; que[min] = qt;
		i = min;
	}
}

//// 本問題関連
#define MAXV 1003
int W, H;
char vis[MAXV][MAXV];
int mv[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int rtbl[MAXV][MAXV], ctbl[MAXV][MAXV];

int dijkstra() {
	int i, r, c, rr, cc, t;

	enq(0, 0, 0);
	while (qsize) {
		r = que[0].r, c = que[0].c, t = que[0].t, deq();
//printf("(%d,%d), t=%d, qsize=%d\n", r, c, t, qsize);
		if (r == H-1 && c == W-1) return t;
		if (vis[r][c]) continue;
		vis[r][c] = 1, t++;
		if (ctbl[r][c] && !vis[r+1][c]) enq(r+1, c, t);
		if (rtbl[r][c] && !vis[r][c+1]) enq(r, c+1, t);
		if (r && ctbl[r-1][c] && !vis[r-1][c]) enq(r-1, c, t);
		if (c && rtbl[r][c-1] && !vis[r][c-1]) enq(r, c-1, t);
	}
	return -1;
}

int main()
{
	int k, N, M, b1, b2, r1, c1, r2, c2;
	
	W = in(), H = in(), N = in();
	if (N == 0) { puts(W*H == 1? "0": "Odekakedekinai.."); return 0; }

	while (N--) {
		M = in(), b1 = in(), r1 = b1/W, c1 = b1%W; while (M--) {
			b2 = in(), r2 = b2/W, c2 = b2%W;
			if (r1 == r2) {
				if (c1 < c2) rtbl[r1][c1]++, rtbl[r1][c2]--;
				else         rtbl[r1][c2]++, rtbl[r1][c1]--;
			} else {
				if (r1 < r2) ctbl[r1][c1]++, ctbl[r2][c1]--;
				else         ctbl[r2][c1]++, ctbl[r1][c1]--;
			}
			r1 = r2, c1 = c2;
		}
	}
	for (r1 = 0; r1 < H; r1++) for (c1 = 1; c1 < W; c1++) rtbl[r1][c1] += rtbl[r1][c1-1];
	for (c1 = 0; c1 < W; c1++) for (r1 = 1; r1 < H; r1++) ctbl[r1][c1] += ctbl[r1-1][c1];

	if ((k = dijkstra()) < 0) puts("Odekakedekinai..");
	else printf("%d\n", k);
	return 0;
}
0