結果

問題 No.34 砂漠の行商人
ユーザー ゆるく
提出日時 2014-10-22 22:16:17
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 108 ms / 5,000 ms
コード長 986 bytes
コンパイル時間 76 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-06-28 10:04:39
合計ジャッジ時間 2,165 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n,hp,sx,sy,gx,gy = map(int, input().split())
e = [[int(i) for i in input().split()] for j in range(n)]
cost = [[0xFFFFFFFF for _ in range(n)] for _ in range(n)]
direction = ((0,1),(0,-1),(1,0),(-1,0))
def bsf():
    q = deque()
    q.append(((sx - 1) << 16) + ((sy - 1) << 8) + 0)
    cost[sy - 1][sx - 1] = 0
    while(q):
        v = q.popleft()
        vx = (v >> 16) & 0xFF
        vy = (v >> 8) & 0xFF
        vc = v & 0xFF
        nowcost = cost[vy][vx]
        if vx == gx - 1 and vy == gy - 1:
            return vc
        
        for d in direction:
            dx = vx + d[1]
            dy = vy + d[0]
            if dx < 0 or dy < 0 or dy >= n or dx >= n:
                continue
            nextcost = nowcost + e[dy][dx]
            if cost[dy][dx] > nextcost and nextcost < hp:
                cost[dy][dx] = nextcost
                q.append((dx << 16) + (dy << 8) + (vc + 1))
    return -1

print(bsf())
0