結果

問題 No.2731 Two Colors
ユーザー sasa8uyauya
提出日時 2024-04-19 22:32:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,583 ms / 3,000 ms
コード長 1,289 bytes
コンパイル時間 228 ms
コンパイル使用メモリ 81,972 KB
実行使用メモリ 120,136 KB
最終ジャッジ日時 2024-10-11 16:08:45
合計ジャッジ時間 19,393 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
v=[[0]*w for i in range(h)]
v[0][0]=1
v[h-1][w-1]=2
u1=[[0]*w for i in range(h)]
u2=[[0]*w for i in range(h)]
u1[0][0]=1
u2[h-1][w-1]=1
from heapq import heappush,heappop
q1=[]
heappush(q1,(a[1][0],1,0))
heappush(q1,(a[0][1],0,1))
q2=[]
heappush(q2,(a[h-2][w-1],h-2,w-1))
heappush(q2,(a[h-1][w-2],h-1,w-2))
for i in range(1,h*w+1):
  if i%2:
    while len(q1)>0 and v[q1[0][1]][q1[0][2]]!=0:
      heappop(q1)
    if len(q1)==0:
      print(i-1)
      exit()
    z,x,y=heappop(q1)
    v[x][y]=1
    dx,dy=1,0
    for _ in range(4):
      xx,yy=x+dx,y+dy
      dx,dy=-dy,dx
      if 0<=xx<h and 0<=yy<w:
        if v[xx][yy]==0 and u1[xx][yy]==0:
          heappush(q1,(a[xx][yy],xx,yy))
          u1[xx][yy]=1
        if v[xx][yy]==2:
          print(i)
          exit()
  else:
    while len(q2)>0 and v[q2[0][1]][q2[0][2]]!=0:
      heappop(q2)
    if len(q2)==0:
      print(i-1)
      exit()
    z,x,y=heappop(q2)
    v[x][y]=2
    dx,dy=1,0
    for _ in range(4):
      xx,yy=x+dx,y+dy
      dx,dy=-dy,dx
      if 0<=xx<h and 0<=yy<w:
        if v[xx][yy]==0 and u2[xx][yy]==0:
          heappush(q2,(a[xx][yy],xx,yy))
          u2[xx][yy]=1
        if v[xx][yy]==1:
          print(i)
          exit()
0