結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,664 KB
testcase_01 AC 17 ms
8,716 KB
testcase_02 AC 19 ms
8,620 KB
testcase_03 AC 18 ms
8,588 KB
testcase_04 AC 27 ms
8,692 KB
testcase_05 AC 29 ms
8,796 KB
testcase_06 AC 24 ms
8,704 KB
testcase_07 AC 43 ms
8,644 KB
testcase_08 AC 48 ms
8,760 KB
testcase_09 AC 41 ms
8,700 KB
testcase_10 AC 28 ms
8,736 KB
testcase_11 AC 37 ms
9,036 KB
testcase_12 AC 22 ms
8,672 KB
testcase_13 AC 87 ms
8,900 KB
testcase_14 AC 85 ms
8,872 KB
testcase_15 AC 19 ms
8,616 KB
testcase_16 AC 25 ms
8,600 KB
testcase_17 AC 20 ms
8,676 KB
testcase_18 AC 19 ms
8,768 KB
testcase_19 AC 46 ms
8,732 KB
testcase_20 AC 63 ms
8,904 KB
testcase_21 AC 21 ms
8,756 KB
testcase_22 AC 22 ms
8,772 KB
testcase_23 AC 19 ms
8,792 KB
testcase_24 AC 67 ms
8,816 KB
testcase_25 AC 25 ms
8,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# bfsだったorz
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 bfs():
    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(bfs())
0