結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  Tawara | 
| 提出日時 | 2015-12-31 02:29:53 | 
| 言語 | Python2 (2.7.18) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 46 ms / 5,000 ms | 
| コード長 | 537 bytes | 
| コンパイル時間 | 255 ms | 
| コンパイル使用メモリ | 7,040 KB | 
| 実行使用メモリ | 8,704 KB | 
| 最終ジャッジ日時 | 2024-09-19 08:55:30 | 
| 合計ジャッジ時間 | 1,742 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
ソースコード
from heapq import heappop,heappush I=lambda:map(int,raw_input().split());W,H=I();G=(W-1,H-1) M=[I()for i in range(H)];dxy=((1,0),(0,1),(-1,0),(0,-1)) C=[[[W*H+1]*10 for i in range(W)]for i in range(H)];Q=[] for x,y in dxy[:2]:C[y][x][M[0][0]]=1;Q+=[(1,(x,y),M[0][0])] while Q: c,h,b=heappop(Q) if h==G:print c;break t=M[h[1]][h[0]];nc=c+1 for dx,dy in dxy: nx=h[0]+dx;ny=h[1]+dy if 0<=nx<W and 0<=ny<H: n=M[ny][nx] if(n-t)*(t-b)<0 and b!=n and nc<C[ny][nx][t]: heappush(Q,(nc,(nx,ny),t));C[ny][nx][t]=nc else: print -1
