結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,516 KB
testcase_01 AC 20 ms
8,748 KB
testcase_02 AC 22 ms
8,680 KB
testcase_03 AC 20 ms
8,532 KB
testcase_04 AC 78 ms
9,004 KB
testcase_05 AC 87 ms
9,092 KB
testcase_06 AC 26 ms
8,620 KB
testcase_07 AC 165 ms
9,068 KB
testcase_08 AC 201 ms
9,312 KB
testcase_09 AC 565 ms
10,220 KB
testcase_10 AC 59 ms
9,076 KB
testcase_11 AC 137 ms
9,228 KB
testcase_12 AC 34 ms
8,864 KB
testcase_13 AC 1,891 ms
12,132 KB
testcase_14 AC 1,275 ms
11,420 KB
testcase_15 AC 22 ms
8,872 KB
testcase_16 AC 78 ms
9,292 KB
testcase_17 AC 20 ms
8,688 KB
testcase_18 AC 23 ms
8,700 KB
testcase_19 AC 445 ms
10,728 KB
testcase_20 AC 721 ms
11,132 KB
testcase_21 AC 22 ms
8,812 KB
testcase_22 AC 27 ms
9,036 KB
testcase_23 AC 21 ms
8,788 KB
testcase_24 AC 870 ms
11,260 KB
testcase_25 AC 59 ms
9,088 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):
    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