結果
問題 |
No.2731 Two Colors
|
ユーザー |
👑 |
提出日時 | 2024-04-19 22:26:37 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,482 bytes |
コンパイル時間 | 506 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 132,276 KB |
最終ジャッジ日時 | 2024-10-11 15:56:48 |
合計ジャッジ時間 | 29,792 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 TLE * 3 |
ソースコード
import heapq def solve(h, w, a): col = [[-1] * w for _ in range(h)] ans = 0 queue0 = [(a[0][0], 0, 0)] queue1 = [(a[h-1][w-1], h-1, w-1)] while True: ans += 1 filled = False while True: if queue0: _, i, j = heapq.heappop(queue0) if col[i][j] != -1: continue filled = True col[i][j] = 0 for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)]: ni, nj = i + di, j + dj if ni < 0 or ni >= h or nj < 0 or nj >= w: continue if col[ni][nj] == 0: continue elif col[ni][nj] == 1: return ans - 2 else: heapq.heappush(queue0, (a[ni][nj], ni, nj)) if filled: break ans += 1 while True: if queue1: _, i, j = heapq.heappop(queue1) if col[i][j] != -1: continue filled = True col[i][j] = 1 for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)]: ni, nj = i + di, j + dj if ni < 0 or ni >= h or nj < 0 or nj >= w: continue if col[ni][nj] == 1: continue elif col[ni][nj] == 0: return ans - 2 else: heapq.heappush(queue1, (a[ni][nj], ni, nj)) if filled: break def main(): h, w = [int(x) for x in input().split()] a = [[int(x) for x in input().split()] for _ in range(h)] print(solve(h, w, a)) if __name__ == "__main__": main()