結果

問題 No.2731 Two Colors
コンテスト
ユーザー rarpipipi
提出日時 2024-04-20 10:47:10
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
TLE  
実行時間 -
コード長 1,039 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 123 ms
コンパイル使用メモリ 84,736 KB
実行使用メモリ 297,444 KB
最終ジャッジ日時 2026-04-28 18:00:52
合計ジャッジ時間 15,474 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from heapq import heapify,heappop,heappush

h,w=map(int,input().split())

a=[list(map(int,input().split())) for i in range(h)]

colored0=set([(0,0)])
colored1=set([(h-1,w-1)])

que0=[]
que1=[]
heappush(que0,(a[0][1],0,1))
heappush(que0,(a[1][0],1,0))
heappush(que1,(a[h-1][w-2],h-1,w-2))
heappush(que1,(a[h-2][w-1],h-2,w-1))
que=[que0,que1]
colored=[colored0,colored1]
walk=[
    (1,0),
    (-1,0),
    (0,1),
    (0,-1),
]
def inside(y,x):
    return 0<=y<h and 0<=x<w

for i in range(h*w):
    t_que=que[i%2]
    t_colored=colored[i%2]
    o_colored=colored[(i%2)^1]
    while t_que:
        _,y,x=heappop(t_que)
        if (y,x) not in t_colored:
            break
    t_colored.add((y,x))
    # 隣がo_coloredで塗られている場合exit
    for iy,ix in walk:
        ny,nx=iy+y,ix+x
        if (ny,nx) in o_colored:
            print(i+1)
            exit()

    for iy,ix in walk:
        ny,nx=iy+y,ix+x
        if inside(ny,nx):
            if not (ny,nx) in t_colored:
                heappush(t_que,(a[ny][nx],ny,nx))






0