結果

問題 No.34 砂漠の行商人
ユーザー ゆるくゆるく
提出日時 2014-10-22 22:16:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 87 ms / 5,000 ms
コード長 986 bytes
コンパイル時間 112 ms
コンパイル使用メモリ 10,832 KB
実行使用メモリ 9,080 KB
最終ジャッジ日時 2023-09-10 18:54:34
合計ジャッジ時間 2,054 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,652 KB
testcase_01 AC 20 ms
8,556 KB
testcase_02 AC 20 ms
8,592 KB
testcase_03 AC 19 ms
8,552 KB
testcase_04 AC 26 ms
8,752 KB
testcase_05 AC 29 ms
8,784 KB
testcase_06 AC 24 ms
8,784 KB
testcase_07 AC 44 ms
8,644 KB
testcase_08 AC 49 ms
8,864 KB
testcase_09 AC 41 ms
8,708 KB
testcase_10 AC 28 ms
8,880 KB
testcase_11 AC 38 ms
9,080 KB
testcase_12 AC 22 ms
8,604 KB
testcase_13 AC 87 ms
8,840 KB
testcase_14 AC 85 ms
8,828 KB
testcase_15 AC 19 ms
8,696 KB
testcase_16 AC 25 ms
8,624 KB
testcase_17 AC 19 ms
8,692 KB
testcase_18 AC 20 ms
8,632 KB
testcase_19 AC 46 ms
8,884 KB
testcase_20 AC 62 ms
8,792 KB
testcase_21 AC 21 ms
8,900 KB
testcase_22 AC 22 ms
8,644 KB
testcase_23 AC 19 ms
8,672 KB
testcase_24 AC 67 ms
8,816 KB
testcase_25 AC 25 ms
8,656 KB
権限があれば一括ダウンロードができます

ソースコード

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