結果
問題 | No.659 徘徊迷路 |
ユーザー | tktk_snsn |
提出日時 | 2021-08-16 06:23:47 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 550 ms / 2,000 ms |
コード長 | 1,948 bytes |
コンパイル時間 | 319 ms |
コンパイル使用メモリ | 82,028 KB |
実行使用メモリ | 77,296 KB |
最終ジャッジ日時 | 2024-10-08 22:49:28 |
合計ジャッジ時間 | 3,838 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
62,588 KB |
testcase_01 | AC | 141 ms
76,180 KB |
testcase_02 | AC | 40 ms
53,264 KB |
testcase_03 | AC | 40 ms
53,380 KB |
testcase_04 | AC | 38 ms
53,284 KB |
testcase_05 | AC | 65 ms
68,700 KB |
testcase_06 | AC | 59 ms
66,704 KB |
testcase_07 | AC | 46 ms
60,456 KB |
testcase_08 | AC | 107 ms
75,048 KB |
testcase_09 | AC | 161 ms
76,612 KB |
testcase_10 | AC | 260 ms
76,816 KB |
testcase_11 | AC | 494 ms
76,880 KB |
testcase_12 | AC | 100 ms
74,424 KB |
testcase_13 | AC | 521 ms
77,296 KB |
testcase_14 | AC | 39 ms
53,572 KB |
testcase_15 | AC | 39 ms
53,220 KB |
testcase_16 | AC | 550 ms
76,948 KB |
ソースコード
def mat_mul(A, B): res = [[0] * len(B[0]) for _ in range(len(A))] for i in range(len(A)): for k in range(len(A[0])): for j in range(len(B[0])): res[i][j] += A[i][k] * B[k][j] return res def mat_pow(A, n): size = len(A) res = [[0] * size for _ in range(size)] for i in range(size): res[i][i] = 1 while n: if n & 1: res = mat_mul(res, A) A = mat_mul(A, A) n >>= 1 return res def main(): dydx4 = ((0, 1), (1, 0), (-1, 0), (0, -1)) h, w, t = map(int, input().split()) sx, sy = map(int, input().split()) gx, gy = map(int, input().split()) S = [input() for _ in range(h)] d = abs(sx-gx)+abs(sy-gy) if (t-d) % 2 == 1 or d > t: return 0.0 blank = [] for x in range(h): for y in range(w): if S[x][y] == ".": blank.append(x*w+y) n = len(blank) btoi = {b: i for i, b in enumerate(blank)} A = [0] * (n+1) G = [[0]*(n+1) for _ in range(n+1)] A[btoi[sx*w+sy]] = 1.0 A[n] = 1.0 for i in range(n+1): G[i][n] = 1. for x in range(h): for y in range(w): if S[x][y] == ".": i = btoi[x*w+y] p = 0 for dx, dy in dydx4: nx = x+dx ny = y+dy p += int(S[nx][ny] == ".") if p == 0: G[i][n] = 0 G[i][i] = 1.0 continue for dx, dy in dydx4: nx = x+dx ny = y+dy if 0 <= nx < h and 0 <= ny < w and S[nx][ny] == ".": j = btoi[nx*w+ny] G[j][i] = 1./p GG = mat_pow(G, t) res = 0. goal = btoi[gx*w+gy] for i in range(n): res += GG[goal][i] * A[i] return res print("{:.18f}".format(main()))