結果

問題 No.659 徘徊迷路
ユーザー brthyyjpbrthyyjp
提出日時 2022-01-18 14:39:39
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,387 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 77,928 KB
最終ジャッジ日時 2024-05-02 19:22:36
合計ジャッジ時間 16,747 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
76,672 KB
testcase_01 AC 450 ms
77,440 KB
testcase_02 AC 80 ms
73,472 KB
testcase_03 AC 44 ms
60,288 KB
testcase_04 AC 639 ms
76,928 KB
testcase_05 AC 104 ms
76,416 KB
testcase_06 AC 100 ms
76,672 KB
testcase_07 AC 89 ms
76,544 KB
testcase_08 AC 736 ms
77,400 KB
testcase_09 AC 1,618 ms
77,632 KB
testcase_10 AC 1,814 ms
77,324 KB
testcase_11 TLE -
testcase_12 AC 393 ms
76,544 KB
testcase_13 AC 1,574 ms
77,636 KB
testcase_14 AC 1,556 ms
77,624 KB
testcase_15 AC 1,890 ms
77,788 KB
testcase_16 AC 1,939 ms
77,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

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

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)]
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

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)
0