結果

問題 No.34 砂漠の行商人
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-08 16:30:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 66 ms / 5,000 ms
コード長 980 bytes
コンパイル時間 145 ms
コンパイル使用メモリ 11,092 KB
実行使用メモリ 8,932 KB
最終ジャッジ日時 2023-09-10 19:10:58
合計ジャッジ時間 1,903 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,384 KB
testcase_01 AC 15 ms
8,016 KB
testcase_02 AC 16 ms
8,388 KB
testcase_03 AC 15 ms
8,272 KB
testcase_04 AC 22 ms
8,408 KB
testcase_05 AC 24 ms
8,428 KB
testcase_06 AC 20 ms
8,312 KB
testcase_07 AC 34 ms
8,252 KB
testcase_08 AC 38 ms
8,592 KB
testcase_09 AC 33 ms
8,524 KB
testcase_10 AC 22 ms
8,552 KB
testcase_11 AC 28 ms
8,696 KB
testcase_12 AC 18 ms
8,428 KB
testcase_13 AC 65 ms
8,932 KB
testcase_14 AC 66 ms
8,864 KB
testcase_15 AC 17 ms
8,348 KB
testcase_16 AC 21 ms
8,460 KB
testcase_17 AC 17 ms
8,352 KB
testcase_18 AC 17 ms
8,264 KB
testcase_19 AC 37 ms
8,512 KB
testcase_20 AC 49 ms
8,664 KB
testcase_21 AC 18 ms
8,388 KB
testcase_22 AC 18 ms
8,232 KB
testcase_23 AC 17 ms
8,316 KB
testcase_24 AC 54 ms
8,856 KB
testcase_25 AC 21 ms
8,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    N, V, Sx, Sy, Gx, Gy = map(int, input().split())
    data = [list(map(int, input().split())) for n in range(N)]
    return N, V, Sx - 1, Sy - 1, Gx - 1, Gy - 1, data


def solve(N, V, Sx, Sy, Gx, Gy, data):
    fs = dict()
    fs[Sx, Sy] = V
    vs = [[0] * N for n in range(N)]
    vs[Sx][Sy] = V
    steps = 0
    while fs:
        steps += 1
        nfs = dict()
        for (x, y), v in fs.items():
            for nx, ny in [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]:
                if nx < 0 or nx >= N or ny < 0 or ny >= N:
                    continue
                newv = v - data[ny][nx]
                if newv <= vs[nx][ny]:
                    continue
                vs[nx][ny] = newv
                nfs[nx, ny] = newv
                if nx == Gx and ny == Gy:
                    return steps
        fs = nfs
    return -1

if __name__ == '__main__':
    N, V, Sx, Sy, Gx, Gy, data = read_data()
    print(solve(N, V, Sx, Sy, Gx, Gy, data))
0