結果

問題 No.659 徘徊迷路
ユーザー tktk_snsntktk_snsn
提出日時 2021-08-16 06:23:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,144 ms / 2,000 ms
コード長 1,948 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-04-17 08:07:55
合計ジャッジ時間 5,683 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
11,008 KB
testcase_01 AC 190 ms
11,392 KB
testcase_02 AC 31 ms
11,008 KB
testcase_03 AC 29 ms
11,008 KB
testcase_04 AC 29 ms
11,008 KB
testcase_05 AC 34 ms
11,008 KB
testcase_06 AC 33 ms
11,008 KB
testcase_07 AC 31 ms
10,880 KB
testcase_08 AC 138 ms
11,264 KB
testcase_09 AC 273 ms
11,264 KB
testcase_10 AC 561 ms
11,520 KB
testcase_11 AC 1,054 ms
11,648 KB
testcase_12 AC 136 ms
11,392 KB
testcase_13 AC 981 ms
11,520 KB
testcase_14 AC 28 ms
11,008 KB
testcase_15 AC 28 ms
11,008 KB
testcase_16 AC 1,144 ms
11,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def mat_mul(A, B):
    res = [[0] * len(B[0]) for _ in range(len(A))]
    for i in range(len(A)):
        for k in range(len(A[0])):
            for j in range(len(B[0])):
                res[i][j] += A[i][k] * B[k][j]
    return res


def mat_pow(A, n):
    size = len(A)
    res = [[0] * size for _ in range(size)]
    for i in range(size):
        res[i][i] = 1
    while n:
        if n & 1:
            res = mat_mul(res, A)
        A = mat_mul(A, A)
        n >>= 1
    return res


def main():
    dydx4 = ((0, 1), (1, 0), (-1, 0), (0, -1))
    h, w, t = map(int, input().split())
    sx, sy = map(int, input().split())
    gx, gy = map(int, input().split())
    S = [input() for _ in range(h)]
    d = abs(sx-gx)+abs(sy-gy)
    if (t-d) % 2 == 1 or d > t:
        return 0.0

    blank = []
    for x in range(h):
        for y in range(w):
            if S[x][y] == ".":
                blank.append(x*w+y)
    n = len(blank)
    btoi = {b: i for i, b in enumerate(blank)}
    A = [0] * (n+1)
    G = [[0]*(n+1) for _ in range(n+1)]
    A[btoi[sx*w+sy]] = 1.0
    A[n] = 1.0
    for i in range(n+1):
        G[i][n] = 1.
    for x in range(h):
        for y in range(w):
            if S[x][y] == ".":
                i = btoi[x*w+y]
                p = 0
                for dx, dy in dydx4:
                    nx = x+dx
                    ny = y+dy
                    p += int(S[nx][ny] == ".")
                if p == 0:
                    G[i][n] = 0
                    G[i][i] = 1.0
                    continue
                for dx, dy in dydx4:
                    nx = x+dx
                    ny = y+dy
                    if 0 <= nx < h and 0 <= ny < w and S[nx][ny] == ".":
                        j = btoi[nx*w+ny]
                        G[j][i] = 1./p
    GG = mat_pow(G, t)
    res = 0.
    goal = btoi[gx*w+gy]
    for i in range(n):
        res += GG[goal][i] * A[i]
    return res


print("{:.18f}".format(main()))
0