結果

問題 No.34 砂漠の行商人
ユーザー しらっ亭しらっ亭
提出日時 2015-08-15 06:01:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 73 ms / 5,000 ms
コード長 1,572 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 10,888 KB
実行使用メモリ 8,924 KB
最終ジャッジ日時 2023-09-10 19:11:29
合計ジャッジ時間 1,993 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,084 KB
testcase_01 AC 16 ms
8,072 KB
testcase_02 AC 16 ms
7,996 KB
testcase_03 AC 15 ms
8,244 KB
testcase_04 AC 22 ms
8,456 KB
testcase_05 AC 24 ms
8,332 KB
testcase_06 AC 22 ms
8,424 KB
testcase_07 AC 37 ms
8,308 KB
testcase_08 AC 41 ms
8,528 KB
testcase_09 AC 35 ms
8,512 KB
testcase_10 AC 24 ms
8,620 KB
testcase_11 AC 30 ms
8,484 KB
testcase_12 AC 18 ms
8,288 KB
testcase_13 AC 73 ms
8,892 KB
testcase_14 AC 71 ms
8,924 KB
testcase_15 AC 17 ms
8,400 KB
testcase_16 AC 23 ms
8,440 KB
testcase_17 AC 17 ms
8,308 KB
testcase_18 AC 17 ms
8,336 KB
testcase_19 AC 39 ms
8,784 KB
testcase_20 AC 52 ms
8,616 KB
testcase_21 AC 18 ms
8,352 KB
testcase_22 AC 19 ms
8,288 KB
testcase_23 AC 17 ms
8,352 KB
testcase_24 AC 58 ms
8,832 KB
testcase_25 AC 22 ms
8,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# http://yukicoder.me/submissions/42588 が凄い! 感動した!

def solve():
    N, V, Sx, Sy, Gx, Gy = list(map(int, input().split()))
    L = []
    for i in range(N):
        L.append(list(map(int, input().split())))
    Sy -= 1
    Sx -= 1
    Gy -= 1
    Gx -= 1

    dy = (-1, 0, 1, 0)
    dx = (0, -1, 0, 1)

    fs = {(Sy, Sx): V}

    vs = [[0 for x in range(N)] for y in range(N)]
    vs[Sy][Sx] = V

    s = 0

    while fs:
        # 1歩ずつ、探す範囲を拡大していく
        s += 1
        # 次に探索する範囲。今いる範囲から1歩広げつつ、今の体力で行ける範囲を探す
        nfs = {}
        for (y, x), v in fs.items():
            for d in range(4):
                ny = y + dy[d]
                nx = x + dx[d]
                if 0 <= ny < N and 0 <= nx < N:
                    nv = v - L[ny][nx]
                    if nv <= vs[ny][nx]:
                        # (ny, nx) に移動したときに残る体力が、過去に歩数が小さくそこに来た時より小さい場合、この先を探索する価値はない
                        continue
                    if ny == Gy and nx == Gx:
                        print(s)
                        return
                    # (ny, nx) に至る最大体力を更新する
                    vs[ny][nx] = nv
                    nfs[ny, nx] = nv
                    # (1, 1) を残体力 2 の歩数 1 で探した後、残耐力 3 の歩数 4 で探したりする
            fs = nfs
    print(-1)


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