結果
問題 |
No.2731 Two Colors
|
ユーザー |
![]() |
提出日時 | 2024-04-19 22:06:44 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,629 bytes |
コンパイル時間 | 395 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 183,272 KB |
最終ジャッジ日時 | 2024-10-11 15:22:40 |
合計ジャッジ時間 | 21,776 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 3 |
other | AC * 6 TLE * 4 -- * 23 |
ソースコード
H, W = map(int, input().split()) A = [] for i in range(H): A.append(list(map(int, input().split()))) from heapq import * def bfs(sx, sy, H, W): dist = [[-1] * W for _ in range(H)] dist[sx][sy] = 0 dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] HH = [(0, sx, sy)] now = 0 seen = [[0] * W for _ in range(H)] seen[sx][sy] = 1 while HH: _, px, py = heappop(HH) dist[px][py] = now now += 1 for k in range(4): x = px + dx[k] y = py + dy[k] if x < 0 or x > H - 1 or y < 0 or y > W - 1: continue if seen[x][y]: continue seen[x][y] = 1 heappush(HH, (A[x][y], x, y)) return dist d1 = bfs(0, 0, H, W) d2 = bfs(H - 1, W - 1, H, W) ans = 10 ** 18 dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] for i in range(H): for j in range(W): now = d1[i][j] flag = 0 for k in range(4): x = i + dx[k] y = j + dy[k] if x < 0 or x > H - 1 or y < 0 or y > W - 1: continue if d2[x][y] < now: flag = 1 break if flag: ans = min(ans, 2 * now - 1) now = d2[i][j] flag = 0 for k in range(4): x = i + dx[k] y = j + dy[k] if x < 0 or x > H - 1 or y < 0 or y > W - 1: continue if d1[x][y] <= now: flag = 1 break if flag: ans = min(ans, 2 * now) print(ans)