結果

問題 No.367 ナイトの転身
ユーザー bal4ubal4u
提出日時 2019-08-20 18:10:46
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 2,007 bytes
コンパイル時間 242 ms
コンパイル使用メモリ 32,512 KB
実行使用メモリ 10,240 KB
最終ジャッジ日時 2024-04-16 02:32:14
合計ジャッジ時間 1,112 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 20 ms
10,240 KB
testcase_11 AC 32 ms
10,240 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 14 ms
7,680 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 15 ms
7,680 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:8:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    8 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:14:24: note: in expansion of macro 'gc'
   14 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: No.367 ナイトの転身
// bal4u 2019.8.20

#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;
}

void ins(char *s) { // 文字列の入力 スペース以下の文字で入力終了
	do *s = gc();
	while (*s++ > ' ');
	*(s-1) = 0;
}

#define QMAX 500000
typedef struct { int r, c, t, knt; } Q;  // knt = knight
Q q[QMAX+5]; int top, end;
int H, W;
char map[505][505];
char vis[505][505][2];
int mk[8][2] = {{-1,-2},{-2, -1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}};
int mb[4][2] = {{-1,-1},{-1,1},{1,1},{1,-1}};

int bfs(int sr, int sc, int gr, int gc) {
	int i, r, c, rr, cc, k, x, t;
	
	q[0].r = sr, q[0].c = sc, q[0].t = 0, q[0].knt = 1;
	top = 0, end = 1;
	while (top != end) {
		r = q[top].r, c = q[top].c, t = q[top].t, k = q[top].knt;
		if (++top == QMAX) top = 0;
		if (r == gr && c == gc) return t;
		if (vis[r][c][k]) continue;
		vis[r][c][k] = 1;
		if (k) {
			for (i = 0; i < 8; i++) {
				rr = r + mk[i][0], cc = c + mk[i][1];
				if (rr < 0 || rr >= H || cc < 0 || cc >= W) continue;
				x = k; if (map[rr][cc] == 'R') x = !k;
				if (vis[rr][cc][x]) continue;
				q[end].r = rr, q[end].c = cc, q[end].t = t+1, q[end].knt = x;
				if (++end == QMAX) end = 0;
			}
		} else {
			for (i = 0; i < 4; i++) {
				rr = r + mb[i][0], cc = c + mb[i][1];
				if (rr < 0 || rr >= H || cc < 0 || cc >= W) continue;
				x = k; if (map[rr][cc] == 'R') x = !k;
				if (vis[rr][cc][x]) continue;
				q[end].r = rr, q[end].c = cc, q[end].t = t+1, q[end].knt = x;
				if (++end == QMAX) end = 0;
			}
		}
	}
	return -1;
}
		
int main()
{
	int r, c, sr, sc, gr, gc;

	H = in(), W = in();
	for (r = 0; r < H; r++) {
		ins(map[r]);
		for (c = 0; c < W; c++) {
			if (map[r][c] == 'S') sr = r, sc = c;
			else if (map[r][c] == 'G') gr = r, gc = c;
		}
	}
	printf("%d\n", bfs(sr, sc, gr, gc));
	return 0;
}
0