結果

問題 No.124 門松列(3)
ユーザー roiti46
提出日時 2015-02-24 18:03:18
言語 Python2
(2.7.18)
結果
TLE  
実行時間 -
コード長 806 bytes
コンパイル時間 113 ms
コンパイル使用メモリ 6,912 KB
実行使用メモリ 133,248 KB
最終ジャッジ日時 2024-06-23 22:56:57
合計ジャッジ時間 12,610 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other TLE * 1 -- * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
sys.setrecursionlimit(100000)
dxy = zip([1,0,-1,0],[0,1,0,-1])
W,H = map(int,raw_input().split())
M = [map(int,raw_input().split()) for i in xrange(H)]
que = deque([[0,0,0,[-1,0,M[0][0]]]])
D = [[10**9]*W for i in xrange(H)]
while que:
    w,h,d,seq = que.popleft()
    if (w,h) == (W-1,H-1):
        print d
        break
    if D[h][w] < d: continue
    D[h][w] = d
    for dx,dy in dxy:
        nw,nh = w+dx,h+dy
        if 0 <= nw < W and 0 <= nh < H:
            if M[nh][nw] not in seq[1:]:
                nseq = seq[1:]
                nseq.append(M[nh][nw])
                med = sorted(nseq)[1]
                if nseq.index(med) == 1: continue
                D[nh][nw] = d+1
                que.append([nw,nh,d+1,seq[1:]+[M[nh][nw]]])
else:
    print -1
0