結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2015-02-24 18:48:18 | 
| 言語 | Python2 (2.7.18) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 44 ms / 5,000 ms | 
| コード長 | 865 bytes | 
| コンパイル時間 | 156 ms | 
| コンパイル使用メモリ | 6,912 KB | 
| 実行使用メモリ | 7,808 KB | 
| 最終ジャッジ日時 | 2024-06-23 23:01:26 | 
| 合計ジャッジ時間 | 1,630 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
ソースコード
import sys
from collections import deque
sys.setrecursionlimit(100000)
dxy = zip([1,0,-1,0],[0,1,0,-1])
def isKadomatsu(a,b,c):
    if a == 0 or b == 0: return True
    return b < a < c or b < c < a or a < c < b or c < a < b
W,H = map(int,raw_input().split())
M = [map(int,raw_input().split()) for i in xrange(H)]
que = deque([[0,0,0]])
cost = [[[-1]*W for i in xrange(H)] for j in xrange(10)]
cost[0][0][0] = 0
while que:
    w,h,bef = que.popleft()
    if (w,h) == (W-1,H-1):
        print cost[bef][h][w]
        break
    cur = M[h][w]
    for dx,dy in dxy:
        nw,nh = w+dx,h+dy
        if not (0 <= nw < W and 0 <= nh < H): continue
        aft = M[nh][nw]
        if not isKadomatsu(bef, cur, aft): continue
        if cost[cur][nh][nw] != -1: continue
        cost[cur][nh][nw] = cost[bef][h][w]+1
        que.append([nw,nh,cur])
else:
    print -1
            
            
            
        