結果

問題 No.124 門松列(3)
ユーザー Tawara
提出日時 2015-12-31 02:47:25
言語 Python2
(2.7.18)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 485 bytes
コンパイル時間 54 ms
コンパイル使用メモリ 7,040 KB
実行使用メモリ 8,704 KB
最終ジャッジ日時 2024-09-19 08:55:32
合計ジャッジ時間 1,321 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

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