結果

問題 No.2731 Two Colors
ユーザー lollipop
提出日時 2024-04-19 22:53:18
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,218 bytes
コンパイル時間 507 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 138,368 KB
最終ジャッジ日時 2024-10-11 16:50:06
合計ジャッジ時間 30,913 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():  

    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:
                print(i+1)
                return 0
            heappush(hq[ii], [A[_x][_y], _x, _y])
        i += 1

if __name__ == '__main__':  
    exit(main())
0