結果
| 問題 |
No.367 ナイトの転身
|
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2019-08-20 18:10:46 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 28 ms / 2,000 ms |
| コード長 | 2,007 bytes |
| コンパイル時間 | 226 ms |
| コンパイル使用メモリ | 31,872 KB |
| 実行使用メモリ | 10,368 KB |
| 最終ジャッジ日時 | 2024-10-06 12:42:20 |
| 合計ジャッジ時間 | 1,271 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
コンパイルメッセージ
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();
| ^~
ソースコード
// 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;
}
bal4u