結果

問題 No.124 門松列(3)
ユーザー roiti46
提出日時 2015-02-24 18:38:04
言語 Python2
(2.7.18)
結果
WA  
実行時間 -
コード長 883 bytes
コンパイル時間 120 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 16,000 KB
最終ジャッジ日時 2024-06-23 22:58:42
合計ジャッジ時間 1,866 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 23 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

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 False if a <= b <= c or c <= b <= a else True
    
W,H = map(int,raw_input().split())
M = [map(int,raw_input().split()) for i in xrange(H)]
que = deque([[0,0,0,M[0][0]]])
cost = [[[[-1]*W for i in xrange(H)] for j in xrange(10)] for k in xrange(10)]
cost[0][M[0][0]][0][0] = 0

while que:
    w,h,b,c = que.popleft()
    if (w,h) == (W-1,H-1):
        print cost[b][c][h][w]
        break
    for dx,dy in dxy:
        nw,nh = w+dx,h+dy
        if not (0 <= nw < W and 0 <= nh < H): continue
        a = M[nh][nw]
        if not isKadomatsu(b, c, a): continue
        if cost[c][a][nh][nw] != -1: continue
        cost[c][a][nh][nw] = cost[b][c][h][w]+1
        que.append([nw,nh,c,a])
else:
    print -1
0