結果

問題 No.659 徘徊迷路
ユーザー nebukuro09nebukuro09
提出日時 2018-03-03 01:14:07
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 2,256 bytes
コンパイル時間 2,127 ms
コンパイル使用メモリ 159,800 KB
実行使用メモリ 8,000 KB
最終ジャッジ日時 2023-09-03 18:58:15
合計ジャッジ時間 4,547 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 WA -
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 61 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 189 ms
7,952 KB
testcase_12 AC 36 ms
4,824 KB
testcase_13 AC 162 ms
7,928 KB
testcase_14 AC 168 ms
7,964 KB
testcase_15 AC 199 ms
7,956 KB
testcase_16 AC 192 ms
7,936 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[nr][nc];
                }
            }
        }
    }


    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