結果

問題 No.34 砂漠の行商人
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-28 11:19:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 721 ms / 5,000 ms
コード長 1,391 bytes
コンパイル時間 540 ms
コンパイル使用メモリ 11,068 KB
実行使用メモリ 11,280 KB
最終ジャッジ日時 2023-09-10 19:24:22
合計ジャッジ時間 3,315 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,752 KB
testcase_01 AC 19 ms
8,740 KB
testcase_02 AC 21 ms
8,756 KB
testcase_03 AC 19 ms
8,764 KB
testcase_04 AC 79 ms
9,008 KB
testcase_05 AC 87 ms
9,032 KB
testcase_06 AC 26 ms
8,668 KB
testcase_07 AC 166 ms
9,180 KB
testcase_08 AC 204 ms
9,388 KB
testcase_09 AC 20 ms
8,688 KB
testcase_10 AC 22 ms
8,844 KB
testcase_11 AC 21 ms
8,644 KB
testcase_12 AC 34 ms
9,040 KB
testcase_13 AC 21 ms
8,716 KB
testcase_14 AC 21 ms
8,748 KB
testcase_15 AC 19 ms
8,776 KB
testcase_16 AC 20 ms
8,656 KB
testcase_17 AC 21 ms
8,828 KB
testcase_18 AC 18 ms
8,744 KB
testcase_19 AC 449 ms
10,596 KB
testcase_20 AC 721 ms
11,280 KB
testcase_21 AC 23 ms
8,932 KB
testcase_22 AC 27 ms
8,884 KB
testcase_23 AC 20 ms
8,648 KB
testcase_24 AC 21 ms
8,744 KB
testcase_25 AC 59 ms
9,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import collections


DELTAS = [(1, 0), (-1, 0), (0, 1), (0, -1)]
IMPOSSIBLE = -1


def solve(size, start_life, start, goal, stage):
    max_cost = max(max(stage_row) for stage_row in stage)
    # 解説を参考に「枝刈り」
    if start_life >= 2 * size * max_cost:
        return sum(abs(s - g) for s, g in zip(start, goal))
    life = [[0 for _ in range(size)] for _ in range(size)]
    life[start[0]][start[1]] = start_life
    q = collections.deque()
    q.append((0, start_life, start))
    while q:
        dist, life0, (r0, c0) = q.popleft()
        if (r0, c0) == goal:
            return dist
        for dr, dc in DELTAS:
            r, c = r0 + dr, c0 + dc
            if r < 0 or c < 0 or r >= size or c >= size:
                continue
            new_life = life0 - stage[r][c]
            if new_life <= life[r][c]:
                continue
            life[r][c] = new_life
            q.append((dist + 1, new_life, (r, c)))
    return IMPOSSIBLE


def main():
    size, start_life, start_c, start_r, goal_c, goal_r = map(int,
                                                             input().split())
    start = (start_r - 1, start_c - 1)
    goal = (goal_r - 1, goal_c - 1)
    stage = [[int(l) for l in input().split()] for _ in range(size)]
    print(solve(size, start_life, start, goal, stage))


if __name__ == '__main__':
    main()
0