結果

問題 No.340 雪の足跡
ユーザー bal4ubal4u
提出日時 2019-07-28 14:11:07
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,924 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 31,388 KB
実行使用メモリ 7,932 KB
最終ジャッジ日時 2023-09-15 11:30:11
合計ジャッジ時間 4,581 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 16 ms
5,084 KB
testcase_15 AC 17 ms
7,108 KB
testcase_16 AC 9 ms
4,708 KB
testcase_17 AC 20 ms
7,264 KB
testcase_18 AC 20 ms
7,156 KB
testcase_19 AC 21 ms
7,188 KB
testcase_20 AC 45 ms
7,872 KB
testcase_21 AC 16 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 WA -
testcase_24 AC 25 ms
7,872 KB
testcase_25 AC 76 ms
7,756 KB
testcase_26 AC 8 ms
4,992 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 13 ms
4,996 KB
testcase_29 AC 143 ms
7,788 KB
testcase_30 AC 105 ms
7,416 KB
testcase_31 AC 210 ms
7,732 KB
testcase_32 AC 234 ms
7,888 KB
testcase_33 AC 225 ms
7,808 KB
testcase_34 AC 233 ms
7,840 KB
testcase_35 AC 224 ms
7,932 KB
testcase_36 AC 223 ms
7,884 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:9:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:15:24: 備考: in expansion of macro ‘gc’
   15 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

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

#include <stdio.h>
#include <string.h>
#include <stdlib.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 1000000
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 to[MAXV][MAXV][4];
char vis[MAXV][MAXV];
int rmi[MAXV], rma[MAXV], cmi[MAXV], cma[MAXV];
int mv[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};

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;
		for (i = 0; i < 4; i++) if (to[r][c][i]) {
			rr = r + mv[i][0], cc = c + mv[i][1];
			if (!vis[rr][cc]) enq(rr, cc, t+1);
		}
	}
	return -1;
}

void rset(int r, int mi, int ma) {
	while (mi < ma) to[r][mi++][1] = 1, to[r][mi][3] = 1;
}

void cset(int c, int mi, int ma) {
	while (mi < ma) to[mi++][c][0] = 1, to[mi][c][2] = 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; }

	memset(rmi, 0x11, sizeof(rmi));
	memset(cmi, 0x11, sizeof(cmi));
	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 (rmi[r1] > rma[r2]) rmi[r1] = rma[r1] = c1;
				else if (c1 > rma[r1]) rma[r1] = c1;
				else if (c1 < rmi[r1]) rmi[r1] = c1;
				if (c2 > rma[r1]) rma[r1] = c2;
				else if (c2 < rmi[r1]) rmi[r1] = c2;
			} else {
				if (cmi[c1] > cma[c2]) cmi[c1] = cma[c1] = r1;
				else if (r1 > cma[c1]) cma[c1] = r1;
				else if (r1 < cmi[c1]) cmi[c1] = r1;
				if (r2 > cma[c1]) cma[c1] = r2;
				else if (r2 < cmi[c1]) cmi[c1] = r2;
			}
			r1 = r2, c1 = c2;
		}
	}
	for (r1 = 0; r1 < H; r1++) rset(r1, rmi[r1], rma[r1]);
	for (c1 = 0; c1 < W; c1++) cset(c1, cmi[c1], cma[c1]);
	
	if ((k = dijkstra()) < 0) puts("Odekakedekinai..");
	else printf("%d\n", k);
	return 0;
}
0