結果

問題 No.34 砂漠の行商人
ユーザー szkhts
提出日時 2021-07-21 14:13:10
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 223 ms / 5,000 ms
コード長 766 bytes
コンパイル時間 105 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-07-17 14:00:26
合計ジャッジ時間 3,327 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
N,V,Sx,Sy,Gx,Gy = map(int,input().split())
Sx -= 1
Sy -= 1
Gx -= 1
Gy -= 1
sand = [list(map(int,input().split())) for i in range(N)]
dxy = [(1,0),(-1,0),(0,-1),(0,1)]
lstimes = [[0]*(N) for i in range(N)]
lscost = [[float('INF')]*N for i in range(N)]
lscost[Sy][Sx] = 0
d = collections.deque([(Sy,Sx)])
while d:
    x,y = d.popleft()
    if x == Gy and y == Gx:
        if lscost[x][y] < V:
            print(lstimes[x][y])
            exit()
    for dx,dy in dxy:
        if 0<= x+dx < N and 0 <= y+dy < N:
            if lscost[x+dx][y+dy] > lscost[x][y] + sand[x+dx][y+dy]:
                lscost[x+dx][y+dy] = lscost[x][y] + sand[x+dx][y+dy]
                lstimes[x+dx][y+dy] = lstimes[x][y]+1
                d.append((x+dx,y+dy))
print(-1)
0