結果

問題 No.2731 Two Colors
ユーザー sasa8uyauyasasa8uyauya
提出日時 2024-04-19 22:32:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,605 ms / 3,000 ms
コード長 1,289 bytes
コンパイル時間 306 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 120,472 KB
最終ジャッジ日時 2024-04-19 22:32:51
合計ジャッジ時間 19,754 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,571 ms
119,856 KB
testcase_01 AC 1,590 ms
120,040 KB
testcase_02 AC 1,605 ms
120,472 KB
testcase_03 AC 146 ms
77,704 KB
testcase_04 AC 209 ms
79,132 KB
testcase_05 AC 183 ms
78,600 KB
testcase_06 AC 270 ms
83,512 KB
testcase_07 AC 243 ms
84,568 KB
testcase_08 AC 161 ms
78,240 KB
testcase_09 AC 517 ms
104,668 KB
testcase_10 AC 146 ms
77,876 KB
testcase_11 AC 889 ms
111,856 KB
testcase_12 AC 469 ms
103,764 KB
testcase_13 AC 752 ms
106,060 KB
testcase_14 AC 318 ms
89,812 KB
testcase_15 AC 165 ms
78,228 KB
testcase_16 AC 407 ms
89,392 KB
testcase_17 AC 477 ms
94,344 KB
testcase_18 AC 207 ms
79,640 KB
testcase_19 AC 436 ms
90,408 KB
testcase_20 AC 320 ms
93,728 KB
testcase_21 AC 272 ms
80,220 KB
testcase_22 AC 642 ms
108,728 KB
testcase_23 AC 284 ms
84,832 KB
testcase_24 AC 240 ms
80,560 KB
testcase_25 AC 129 ms
77,320 KB
testcase_26 AC 465 ms
100,480 KB
testcase_27 AC 593 ms
107,320 KB
testcase_28 AC 530 ms
98,412 KB
testcase_29 AC 263 ms
82,812 KB
testcase_30 AC 327 ms
87,272 KB
testcase_31 AC 328 ms
84,376 KB
testcase_32 AC 855 ms
118,396 KB
testcase_33 AC 40 ms
52,444 KB
testcase_34 AC 41 ms
52,608 KB
testcase_35 AC 49 ms
52,736 KB
権限があれば一括ダウンロードができます

ソースコード

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