結果

問題 No.367 ナイトの転身
ユーザー nebukuro09nebukuro09
提出日時 2019-02-05 07:46:46
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 640 ms / 2,000 ms
コード長 1,677 bytes
コンパイル時間 814 ms
コンパイル使用メモリ 121,684 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2023-09-03 23:37:21
合計ジャッジ時間 4,253 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 290 ms
6,824 KB
testcase_11 AC 640 ms
5,672 KB
testcase_12 AC 183 ms
4,376 KB
testcase_13 AC 171 ms
4,376 KB
testcase_14 AC 168 ms
4,376 KB
testcase_15 AC 6 ms
4,376 KB
testcase_16 AC 150 ms
4,376 KB
testcase_17 AC 18 ms
4,376 KB
testcase_18 AC 26 ms
4,376 KB
testcase_19 AC 47 ms
4,380 KB
testcase_20 AC 21 ms
4,380 KB
testcase_21 AC 45 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 3 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.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

const int[][] dr = [[-2, -2, -1, -1, 1, 1, 2, 2], [-1, -1, 1, 1]];
const int[][] dc = [[-1, 1, -2, 2, -2, 2, -1, 1], [-1, 1, -1, 1]];

void main() {
    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto A = H.iota.map!(_ => readln.chomp).array;

    int sr, sc, gr, gc;

    foreach (i; 0..H) {
        foreach (j; 0..W) {
            if (A[i][j] == 'S') {
                sr = i;
                sc = j;
            } else if (A[i][j] == 'G') {
                gr = i;
                gc = j;
            }
        }
    }


    auto dist = new int[][][](2, H, W);
    foreach (i; 0..2) foreach (j; 0..H) dist[i][j][] = 1 << 29;

    auto pq = new BinaryHeap!(Array!(Tuple!(int, int, int, int)), "a[3] > b[3]");
    pq.insert(tuple(0, sr, sc, 0));

    while (!pq.empty) {
        int k = pq.front[0];
        int r = pq.front[1];
        int c = pq.front[2];
        int d = pq.front[3];
        pq.removeFront;
        if (dist[k][r][c] <= d) continue;
        dist[k][r][c] = d;

        foreach (i; 0..dr[k].length) {
            int nr = r + dr[k][i];
            int nc = c + dc[k][i];
            if (nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
            int nk = A[nr][nc] == 'R' ? k ^ 1 : k;
            int nd = d + 1;
            if (dist[nk][nr][nc] <= nd) continue;
            pq.insert(tuple(nk, nr, nc, nd));
        }
    }

    int ans = min(dist[0][gr][gc], dist[1][gr][gc]);
    writeln(ans == 1 << 29 ? -1 : ans);
}
0