結果
| 問題 |
No.2731 Two Colors
|
| コンテスト | |
| ユーザー |
detteiuu
|
| 提出日時 | 2024-04-19 22:16:43 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,560 ms / 3,000 ms |
| コード長 | 1,372 bytes |
| コンパイル時間 | 254 ms |
| コンパイル使用メモリ | 82,012 KB |
| 実行使用メモリ | 119,592 KB |
| 最終ジャッジ日時 | 2024-10-11 15:42:29 |
| 合計ジャッジ時間 | 18,496 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
from heapq import heappush, heappop, heapify
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
if H==2 and W==2:
exit(print(1))
color0 = [[-1]*W for _ in range(H)]
color1 = [[-1]*W for _ in range(H)]
color0[0][0] = 0
color1[H-1][W-1] = 1
que0 = [(A[1][0], 1, 0), (A[0][1], 0, 1)]
color0[1][0] = 0
color0[0][1] = 0
que1 = [(A[H-2][W-1], H-2, W-1), (A[H-1][W-2], H-1, W-2)]
color1[H-2][W-1] = 1
color1[H-1][W-2] = 1
B = [[-1]*W for _ in range(H)]
B[0][0] = 0
B[H-1][W-1] = 1
heapify(que0)
heapify(que1)
direction = [(-1,0),(0,1),(1,0),(0,-1)]
cnt = 0
while True:
if cnt%2 == 0:
n, h, w = heappop(que0)
else:
n, h, w = heappop(que1)
B[h][w] = cnt%2
for dh, dw in direction:
if cnt%2 == 0:
if 0<=h+dh<H and 0<=w+dw<W and color0[h+dh][w+dw]==-1 and B[h+dh][w+dw]!=1:
color0[h+dh][w+dw] = 0
heappush(que0, (A[h+dh][w+dw], h+dh, w+dw))
elif 0<=h+dh<H and 0<=w+dw<W and B[h+dh][w+dw] == 1:
exit(print(cnt+1))
else:
if 0<=h+dh<H and 0<=w+dw<W and color1[h+dh][w+dw]==-1 and B[h+dh][w+dw]!=0:
color1[h+dh][w+dw] = 1
heappush(que1, (A[h+dh][w+dw], h+dh, w+dw))
elif 0<=h+dh<H and 0<=w+dw<W and B[h+dh][w+dw] == 0:
exit(print(cnt+1))
cnt += 1
detteiuu