結果

問題 No.659 徘徊迷路
ユーザー rlangevinrlangevin
提出日時 2023-10-18 12:42:46
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,529 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 81,808 KB
実行使用メモリ 79,252 KB
最終ジャッジ日時 2023-10-18 12:43:04
合計ジャッジ時間 17,610 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
72,512 KB
testcase_01 AC 463 ms
76,536 KB
testcase_02 AC 61 ms
68,360 KB
testcase_03 AC 47 ms
61,808 KB
testcase_04 AC 403 ms
75,800 KB
testcase_05 AC 84 ms
75,632 KB
testcase_06 AC 85 ms
75,672 KB
testcase_07 AC 61 ms
68,392 KB
testcase_08 AC 731 ms
76,996 KB
testcase_09 AC 1,668 ms
78,812 KB
testcase_10 AC 1,978 ms
79,232 KB
testcase_11 TLE -
testcase_12 AC 378 ms
76,324 KB
testcase_13 AC 1,686 ms
78,812 KB
testcase_14 AC 1,714 ms
78,812 KB
testcase_15 TLE -
testcase_16 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def Matprod(A, B, N):
    temp = [0] * N*N
    for i in range(N):
        for j in range(N):
            ij = i * N + j
            for k in range(N):
                temp[ij] += A[i*N+k] * B[k*N+j]
    return temp

def Matpow_Linear(A, M, N):
    Mat = [0] * N*N
    for i in range(N):
        Mat[i*N+i] = 1
         
    while M:
        if M & 1:
            Mat = Matprod(Mat, A, N)
        A = Matprod(A, A, N)
        M >>= 1
    return Mat  

def main():
    H, W, T = map(int, input().split())
    sx, sy = map(int, input().split())
    gx, gy = map(int, input().split())
    N = H * W
    M = [0] * N * N
    B = []
    for i in range(H):
        B.extend(list(input().rstrip()))

    dx = [-1, 0, 1, 0]
    dy = [0, -1, 0, 1]

    def f(h, w):
        return h * W + w

    for i in range(H):
        for j in range(W):
            v = f(i,j)
            if B[v] == "#":
                continue
            cnt = 0
            for k in range(4):
                x = i + dx[k]
                y = j + dy[k]
                if B[f(x,y)] == "#":
                    continue
                cnt += 1
            if cnt == 0:
                M[v * N + v] = 1
            else:
                for k in range(4):
                    x = i + dx[k]
                    y = j + dy[k]
                    if B[f(x,y)] == "#":
                        continue
                    M[f(x,y) * N + v] += 1 / cnt

    M = Matpow_Linear(M, T, N)
    print(M[f(gx, gy) * N + f(sx, sy)])

main()
0