結果
問題 | No.659 徘徊迷路 |
ユーザー | tktk_snsn |
提出日時 | 2021-08-16 06:23:18 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,243 ms / 2,000 ms |
コード長 | 1,948 bytes |
コンパイル時間 | 389 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 11,264 KB |
最終ジャッジ日時 | 2024-10-08 22:49:19 |
合計ジャッジ時間 | 6,353 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 36 ms
10,880 KB |
testcase_01 | AC | 194 ms
11,136 KB |
testcase_02 | AC | 33 ms
10,752 KB |
testcase_03 | AC | 33 ms
10,880 KB |
testcase_04 | AC | 34 ms
10,880 KB |
testcase_05 | AC | 36 ms
10,880 KB |
testcase_06 | AC | 34 ms
10,752 KB |
testcase_07 | AC | 33 ms
10,752 KB |
testcase_08 | AC | 143 ms
11,136 KB |
testcase_09 | AC | 296 ms
11,136 KB |
testcase_10 | AC | 604 ms
11,008 KB |
testcase_11 | AC | 1,159 ms
11,264 KB |
testcase_12 | AC | 142 ms
11,008 KB |
testcase_13 | AC | 1,053 ms
11,136 KB |
testcase_14 | AC | 31 ms
10,880 KB |
testcase_15 | AC | 31 ms
10,752 KB |
testcase_16 | AC | 1,243 ms
11,136 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()))