結果

問題 No.2731 Two Colors
ユーザー lollipop
提出日時 2024-04-19 22:59:20
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 997 bytes
コンパイル時間 1,011 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 70,400 KB
最終ジャッジ日時 2024-10-11 17:03:41
合計ジャッジ時間 9,890 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

dxy = [(1,0), (-1,0), (0,1), (0,-1)]
from heapq import heapify, heappop, heappush

H, W = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(H)]

visited = [[None for _ in range(W)] for _ in range(H)]
visited[0][0] = 0
visited[H-1][W-1] = 1

hq = [[], []]
heapify(hq[0]); heapify(hq[1])
for i, xy in enumerate([[0, 0], [H-1, W-1]]):
    x, y = xy
    for dx, dy in dxy:
        _x = x+dx
        _y = y+dy
        if (not 0 <= _x < H) or (not 0 <= _y < W):
            continue
        heappush(hq[i], [A[_x][_y], _x, _y])


i = 0
while True:
    ii = i % 2
    a, x, y = heappop(hq[ii])
    if visited[x][y] != None:
        continue
    visited[x][y] = ii

    for dx, dy in dxy:
        _x = x+dx
        _y = y+dy
        if (not 0 <= _x < H) or (not 0 <= _y < W):
            continue
        if visited[_x][_y] == ii:
            continue
        if visited[_x][_y] == (ii+1) % 2:
            exit(print(i+1))
        heappush(hq[ii], [A[_x][_y], _x, _y])
    i += 1
0