結果

問題 No.367 ナイトの転身
ユーザー kokatsu
提出日時 2022-12-23 19:22:39
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 1,369 bytes
コンパイル時間 1,942 ms
コンパイル使用メモリ 217,016 KB
実行使用メモリ 28,360 KB
最終ジャッジ日時 2024-06-22 17:06:33
合計ジャッジ時間 3,895 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

enum int[] kx = [-2, -1, 1, 2, 2, 1, -1, -2];
enum int[] ky = [1, 2, 2, 1, -1, -2, -2, -1];

enum int[] bx = [-1, 1, 1, -1];
enum int[] by = [1, 1, -1, -1];

struct Piece {
    int x, y, b;
}

void main() {
    int H, W;
    readf("%d %d\n", H, W);

    Piece[] que;
    int gx, gy;

    auto dists = new int[][][](H, W, 2);
    foreach (i; 0 .. H) {
        foreach (j; 0 .. W) {
            dists[i][j][] = int.max;
        }
    }

    auto s = new string[](H);
    foreach (i; 0 .. H) {
        readf("%s\n", s[i]);

        foreach (j; 0 .. W) {
            if (s[i][j] == 'S') que ~= Piece(i, j, 0), dists[i][j][0] = 0;
            if (s[i][j] == 'G') gx = i, gy = j;
        }
    }

    while (!que.empty) {
        auto f = que.front;
        que.popFront;

        int[] dx = (f.b == 0 ? kx : bx);
        int[] dy = (f.b == 0 ? ky : by);

        foreach (u, v; zip(dx, dy)) {
            Piece g = f;
            g.x += u, g.y += v;
            if (g.x < 0 || H <= g.x || g.y < 0 || W <= g.y) continue;

            if (s[g.x][g.y] == 'R') g.b = (g.b + 1) % 2;

            if (dists[g.x][g.y][g.b] > dists[f.x][f.y][f.b] + 1) {
                dists[g.x][g.y][g.b] = dists[f.x][f.y][f.b] + 1;
                que ~= g;
            }
        }
    }

    int res = dists[gx][gy].minElement;
    if (res == int.max) res = -1;

    res.writeln;
}
0