結果

問題 No.124 門松列(3)
コンテスト
ユーザー Tawara
提出日時 2015-12-31 02:47:25
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 121 ms / 5,000 ms
コード長 485 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 151 ms
コンパイル使用メモリ 77,460 KB
最終ジャッジ日時 2025-12-03 18:59:11
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from heapq import heappop,heappush
I=lambda:map(int,raw_input().split());W,H=I()
M=[I()for i in range(H)];D=((1,0),(0,1),(-1,0),(0,-1))
C=[[10*[W*H]for i in range(W)]for i in range(H)];Q=[]
for x,y in D[:2]:m=M[0][0];C[y][x][m]=1;Q+=[(1,x,y,m)]
while Q:
	c,x,y,b=heappop(Q)
	if(W-x)*(H-y)==1:print c;break
	t=M[y][x];d=c+1
	for u,v in D:
		z=x+u;w=y+v
		if 0<=z<W and 0<=w<H:
			n=M[w][z]
			if(n-t)*(t-b)<0 and b^n and d<C[w][z][t]:
				heappush(Q,(d,z,w,t));C[w][z][t]=d
else:print-1
0