結果
問題 | No.2731 Two Colors |
ユーザー |
![]() |
提出日時 | 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 |
ソースコード
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]=1v[h-1][w-1]=2u1=[[0]*w for i in range(h)]u2=[[0]*w for i in range(h)]u1[0][0]=1u2[h-1][w-1]=1from heapq import heappush,heappopq1=[]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]=1dx,dy=1,0for _ in range(4):xx,yy=x+dx,y+dydx,dy=-dy,dxif 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]=1if 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]=2dx,dy=1,0for _ in range(4):xx,yy=x+dx,y+dydx,dy=-dy,dxif 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]=1if v[xx][yy]==1:print(i)exit()