結果

問題 No.659 徘徊迷路
ユーザー nebukuro09nebukuro09
提出日時 2018-03-03 01:20:40
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 2,277 bytes
コンパイル時間 1,849 ms
コンパイル使用メモリ 159,640 KB
実行使用メモリ 7,976 KB
最終ジャッジ日時 2023-09-03 18:58:24
合計ジャッジ時間 4,171 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 41 ms
5,324 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 60 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 69 ms
6,600 KB
testcase_09 AC 168 ms
7,908 KB
testcase_10 AC 189 ms
7,976 KB
testcase_11 AC 191 ms
7,936 KB
testcase_12 AC 35 ms
4,892 KB
testcase_13 AC 164 ms
7,952 KB
testcase_14 AC 168 ms
7,948 KB
testcase_15 AC 196 ms
7,936 KB
testcase_16 AC 198 ms
7,872 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, std.bitmanip;


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

    int[] dr = [0, 0, 1, -1];
    int[] dc = [1, -1, 0, 0];

    auto cnt = new int[][](H, W);
    auto mat = new real[][](H*W, H*W);
    foreach (i; 0..H*W) foreach (j; 0..H*W) mat[i][j] = 0;

    foreach (r; 0..H) {
        foreach (c; 0..W) {
            if (A[r][c] == '#') continue;
            foreach (i; 0..4) {
                int nr = r + dr[i];
                int nc = c + dc[i];
                if (nr >= 0 && nr < H && nc >= 0 && nc < W && A[nr][nc] == '.') {
                    cnt[r][c] += 1;
                }
            }
        }
    }

    foreach (r; 0..H) {
        foreach (c; 0..W) {
            if (A[r][c] == '#') continue;
            if (cnt[r][c] == 0) {
                mat[r*W + c][r*W + c] = 1;
                continue;
            }
            foreach (i; 0..4) {
                int nr = r + dr[i];
                int nc = c + dc[i];
                if (nr >= 0 && nr < H && nc >= 0 && nc < W && A[nr][nc] == '.') {
                    mat[r*W + c][nr*W + nc] = 1.0L / cnt[r][c];
                }
            }
        }
    }

    //mat.each!writeln;
    auto ans = powmod(mat, T);
    writefln("%.09f", ans[sr*W + sc][gr*W + gc]);
}

real[][] mul(real[][] A, real[][] B) {
    auto n = A.length.to!int;
    auto ret = new real[][](n, n);
    foreach (i; 0..n) foreach (j; 0..n) ret[i][j] = 0;
    foreach (i; 0..n)
        foreach (j; 0..n)
            foreach (k; 0..n)
                ret[i][j] += A[i][k] * B[k][j];
    return ret;
}

real[][] powmod(real[][] A, long x) {
    auto n = A.length.to!int;
    auto ret = new real[][](n, n);
    foreach (i; 0..n) foreach (j; 0..n) ret[i][j] = i == j ? 1 : 0;
    while (x) {
        if (x % 2) ret = mul(ret, A);
        A = mul(A, A);
        x /= 2;
    }
    return ret;
}
0