結果

問題 No.659 徘徊迷路
ユーザー rlangevinrlangevin
提出日時 2023-10-18 12:50:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,237 ms / 2,000 ms
コード長 1,629 bytes
コンパイル時間 218 ms
コンパイル使用メモリ 81,676 KB
実行使用メモリ 77,512 KB
最終ジャッジ日時 2023-10-18 12:50:38
合計ジャッジ時間 8,155 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
72,504 KB
testcase_01 AC 491 ms
76,448 KB
testcase_02 AC 39 ms
52,168 KB
testcase_03 AC 45 ms
59,584 KB
testcase_04 AC 39 ms
52,168 KB
testcase_05 AC 92 ms
75,616 KB
testcase_06 AC 87 ms
75,660 KB
testcase_07 AC 62 ms
68,384 KB
testcase_08 AC 708 ms
76,992 KB
testcase_09 AC 741 ms
76,992 KB
testcase_10 AC 1,038 ms
77,492 KB
testcase_11 AC 1,190 ms
77,512 KB
testcase_12 AC 383 ms
76,320 KB
testcase_13 AC 786 ms
76,916 KB
testcase_14 AC 38 ms
53,512 KB
testcase_15 AC 37 ms
53,512 KB
testcase_16 AC 1,237 ms
77,512 KB
権限があれば一括ダウンロードができます

ソースコード

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
         
    pre = Mat[f(gx, gy) * N + f(sx, sy)]
    while M:
        if M & 1:
            Mat = Matprod(Mat, A, N)
            nex = Mat[f(gx, gy) * N + f(sx, sy)]
            if abs(pre - nex) < 1e-7 and nex > 1e-7:
                return Mat
            pre = nex
        A = Matprod(A, A, N)
        
        M >>= 1
    return Mat  


H, W, T = map(int, input().split())
sx, sy = map(int, input().split())
gx, gy = map(int, input().split())
if (sx + sy + gx + gy + T) % 2:
    print(0)
    exit()
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)])

0