結果

問題 No.659 徘徊迷路
ユーザー brthyyjp
提出日時 2022-01-18 14:44:16
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,368 bytes
コンパイル時間 102 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 44,680 KB
最終ジャッジ日時 2024-11-23 13:21:48
合計ジャッジ時間 11,319 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 5
other RE * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np

#A**n
def mat_pow(A, n):
    size = len(A)
    res = np.eye(size, dtype=np.float)
    while n > 0:
        if n & 1 == 1:
            res = res @ A
        A = A @ A
        n = n>>1
    return res

import sys
import io, os
input = sys.stdin.readline

def main():

    r, c, t = map(int, input().split())
    sy, sx = map(int, input().split())
    gy, gx = map(int, input().split())

    B = []
    for i in range(r):
        temp = str(input().rstrip())
        B.append(temp)
    B = ''.join(B)

    A = [[0]*(r*c) for i in range(r*c)]
    A = np.array(A, dtype=np.float)
    for y in range(r):
        for x in range(c):
            v = y*c+x
            if B[v] == '#':
                continue
            p = 0
            cand = []
            for dy, dx in (-1, 0), (1, 0), (0, 1), (0, -1):
                ny, nx = y+dy, x+dx
                nv = ny*c+nx
                if 0 <= ny < r and 0 <= nx < c and B[nv] != '#':
                    p += 1
                    cand.append(nv)
            if p == 0:
                A[v][v] = 1
                continue
            for nv in cand:
                A[nv][v] += 1/p
    #print(A)
    sv = sy*c+sx
    gv = gy*c+gx
    X = [0]*(r*c)
    X[sv] = 1
    A = mat_pow(A, t)
    ans = 0
    for v in range(v):
        ans += A[gv][v]*X[v]
    print(ans)

if __name__ == '__main__':
    main()
0