結果

問題 No.659 徘徊迷路
ユーザー mkawa2
提出日時 2020-08-07 07:52:23
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,773 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 82,312 KB
実行使用メモリ 83,740 KB
最終ジャッジ日時 2024-09-22 14:57:07
合計ジャッジ時間 19,844 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 8 TLE * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 6)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def dot(aa,bb):
    return [[sum(a*b for a,b in zip(row,col)) for col in zip(*bb)] for row in aa]

def solve(t):
    pp = [[0] * w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if aa[i][j]: continue
            for di, dj in dij:
                ni, nj = di + i, dj + j
                if ni < 0 or nj < 0 or ni >= h or nj >= w: continue
                pp[i][j] += 1 - aa[ni][nj]

    if pp[si][sj] == 0:
        print(1)
        exit()

    bb = [[0] * h * w for _ in range(h * w)]
    for i in range(h):
        for j in range(w):
            for di, dj in dij:
                ni, nj = di + i, dj + j
                if ni < 0 or nj < 0 or ni >= h or nj >= w: continue
                if pp[ni][nj]: bb[ni * w + nj][i * w + j] = 1 / pp[ni][nj]

    cc = [[0] * h * w for _ in range(h * w)]
    for i in range(h * w): cc[i][i] = 1
    while t:
        if t & 1:
            cc = dot(cc, bb)
        bb = dot(bb, bb)
        t >>= 1

    ans = [[0] * h * w]
    ans[0][si * w + sj] = 1
    ans = dot(ans, cc)
    print(ans[0][gi * w + gj])

h,w,t=MI()
si,sj=MI()
gi,gj=MI()
aa=[[c=="#" for c in SI()] for _ in range(h)]

solve(t)
0