結果

問題 No.367 ナイトの転身
ユーザー kokatsukokatsu
提出日時 2022-12-23 19:22:39
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 224 ms / 2,000 ms
コード長 1,369 bytes
コンパイル時間 1,844 ms
コンパイル使用メモリ 213,136 KB
実行使用メモリ 29,260 KB
最終ジャッジ日時 2023-09-04 19:24:52
合計ジャッジ時間 4,077 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 146 ms
25,036 KB
testcase_11 AC 224 ms
29,260 KB
testcase_12 AC 102 ms
13,216 KB
testcase_13 AC 94 ms
13,968 KB
testcase_14 AC 92 ms
12,908 KB
testcase_15 AC 5 ms
4,984 KB
testcase_16 AC 83 ms
11,708 KB
testcase_17 AC 11 ms
4,656 KB
testcase_18 AC 15 ms
4,708 KB
testcase_19 AC 25 ms
8,344 KB
testcase_20 AC 11 ms
6,492 KB
testcase_21 AC 26 ms
8,816 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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