結果

問題 No.34 砂漠の行商人
ユーザー しらっ亭しらっ亭
提出日時 2015-08-15 06:01:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 88 ms / 5,000 ms
コード長 1,572 bytes
コンパイル時間 81 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-06-28 10:20:19
合計ジャッジ時間 2,037 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,752 KB
testcase_01 AC 28 ms
10,880 KB
testcase_02 AC 30 ms
10,752 KB
testcase_03 AC 29 ms
10,752 KB
testcase_04 AC 36 ms
10,880 KB
testcase_05 AC 39 ms
10,752 KB
testcase_06 AC 35 ms
10,752 KB
testcase_07 AC 50 ms
10,880 KB
testcase_08 AC 53 ms
10,880 KB
testcase_09 AC 50 ms
11,008 KB
testcase_10 AC 38 ms
11,008 KB
testcase_11 AC 43 ms
11,264 KB
testcase_12 AC 31 ms
10,752 KB
testcase_13 AC 86 ms
11,392 KB
testcase_14 AC 88 ms
11,264 KB
testcase_15 AC 30 ms
10,752 KB
testcase_16 AC 36 ms
10,752 KB
testcase_17 AC 31 ms
10,752 KB
testcase_18 AC 30 ms
10,880 KB
testcase_19 AC 53 ms
11,264 KB
testcase_20 AC 66 ms
11,264 KB
testcase_21 AC 33 ms
10,880 KB
testcase_22 AC 33 ms
10,880 KB
testcase_23 AC 31 ms
10,752 KB
testcase_24 AC 74 ms
11,136 KB
testcase_25 AC 35 ms
10,752 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