結果

問題 No.34 砂漠の行商人
ユーザー しらっ亭しらっ亭
提出日時 2015-08-15 05:58:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,571 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 10,956 KB
実行使用メモリ 16,344 KB
最終ジャッジ日時 2023-09-25 11:55:13
合計ジャッジ時間 10,341 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 21 ms
7,964 KB
testcase_03 AC 16 ms
8,324 KB
testcase_04 AC 43 ms
8,476 KB
testcase_05 WA -
testcase_06 AC 40 ms
8,600 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 187 ms
9,184 KB
testcase_10 AC 121 ms
8,928 KB
testcase_11 AC 30 ms
8,528 KB
testcase_12 AC 23 ms
8,336 KB
testcase_13 AC 525 ms
9,384 KB
testcase_14 AC 436 ms
9,596 KB
testcase_15 AC 18 ms
8,256 KB
testcase_16 AC 43 ms
8,540 KB
testcase_17 AC 18 ms
8,272 KB
testcase_18 AC 18 ms
8,284 KB
testcase_19 AC 138 ms
9,276 KB
testcase_20 AC 215 ms
9,400 KB
testcase_21 TLE -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

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