結果

問題 No.34 砂漠の行商人
ユーザー bal4ubal4u
提出日時 2019-06-30 08:53:42
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 710 ms / 5,000 ms
コード長 1,392 bytes
コンパイル時間 1,202 ms
コンパイル使用メモリ 30,160 KB
実行使用メモリ 105,248 KB
最終ジャッジ日時 2023-09-18 14:54:30
合計ジャッジ時間 5,702 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 3 ms
4,904 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 16 ms
19,348 KB
testcase_05 AC 19 ms
21,096 KB
testcase_06 AC 6 ms
8,008 KB
testcase_07 AC 32 ms
32,656 KB
testcase_08 AC 39 ms
41,112 KB
testcase_09 AC 230 ms
94,948 KB
testcase_10 AC 35 ms
38,268 KB
testcase_11 AC 607 ms
105,248 KB
testcase_12 AC 10 ms
12,912 KB
testcase_13 AC 710 ms
105,228 KB
testcase_14 AC 560 ms
104,112 KB
testcase_15 AC 3 ms
6,672 KB
testcase_16 AC 43 ms
50,200 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 6 ms
8,996 KB
testcase_19 AC 184 ms
98,916 KB
testcase_20 AC 303 ms
101,692 KB
testcase_21 AC 3 ms
4,900 KB
testcase_22 AC 7 ms
9,924 KB
testcase_23 AC 3 ms
6,468 KB
testcase_24 AC 404 ms
102,656 KB
testcase_25 AC 29 ms
33,620 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.34 砂漠の行商人
// 2019.6.30 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 QMAX 5000000
typedef struct { int r, c, v, t; } Q;
Q q[QMAX+5]; int top, end;
int N;
char map[103][103];
int mv[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};
unsigned vis[103][103][626];

int bfs(int V, int sr, int sc, int gr, int gc) {
	int i, r, c, v, t, rr, cc;
	
	top = 0, end = 1;
	q[0].r = sr, q[0].c = sc, q[0].v = V, q[0].t = 0;
	while (top != end) {
		r = q[top].r, c = q[top].c, v = q[top].v, t = q[top].t;
		if (++top == QMAX) top = 0;
		if (r == gr && c == gc) return t;
		if (vis[r][c][v>>4] & (1<<(v&15))) continue;
		vis[r][c][v>>4] |= 1<<(v&15);
		for (i = 0; i < 4; i++) {
			rr = r + mv[i][0], cc = c + mv[i][1];
			if (rr < 0 || rr >= N || cc < 0 || cc >= N) continue;
			if (v <= map[rr][cc]) continue;
			q[end].r = rr, q[end].c = cc, q[end].v = v-map[rr][cc], q[end].t = t+1;
			if (++end == QMAX) end = 0;
		}
	}
	return -1;
}
		
int main()
{
	int r, c, V, sr, sc, gr, gc;

	N = in(), V = in();
	sc = in()-1, sr = in()-1, gc = in()-1, gr = in()-1;
	for (r = 0; r < N; r++) for (c = 0; c < N; c++) {
		map[r][c] = gc() & 0xf, gc();
	}
	printf("%d\n", bfs(V, sr, sc, gr, gc));
	return 0;
}
0