結果

問題 No.34 砂漠の行商人
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-12-31 22:10:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 275 ms / 5,000 ms
コード長 983 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 237,328 KB
最終ジャッジ日時 2024-04-17 10:38:22
合計ジャッジ時間 4,077 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,760 KB
testcase_01 AC 41 ms
54,144 KB
testcase_02 AC 57 ms
65,792 KB
testcase_03 AC 52 ms
64,000 KB
testcase_04 AC 63 ms
71,424 KB
testcase_05 AC 66 ms
73,244 KB
testcase_06 AC 64 ms
70,272 KB
testcase_07 AC 81 ms
81,792 KB
testcase_08 AC 90 ms
85,760 KB
testcase_09 AC 156 ms
158,444 KB
testcase_10 AC 275 ms
237,328 KB
testcase_11 AC 207 ms
226,948 KB
testcase_12 AC 61 ms
70,784 KB
testcase_13 AC 231 ms
234,788 KB
testcase_14 AC 226 ms
226,816 KB
testcase_15 AC 106 ms
112,768 KB
testcase_16 AC 105 ms
112,768 KB
testcase_17 AC 124 ms
131,684 KB
testcase_18 AC 73 ms
86,992 KB
testcase_19 AC 114 ms
112,000 KB
testcase_20 AC 153 ms
145,920 KB
testcase_21 AC 55 ms
69,632 KB
testcase_22 AC 146 ms
146,944 KB
testcase_23 AC 129 ms
135,600 KB
testcase_24 AC 220 ms
219,520 KB
testcase_25 AC 89 ms
88,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n,v,sy,sx,gy,gx = map(int,input().split())
L = [list(map(int,input().split())) for i in range(n)]
sx,sy,gx,gy = sx-1,sy-1,gx-1,gy-1
inf = 10**10
M = min(v,2000)
dp = [[inf]*(n*n) for i in range(M+1)]
ans = [inf]*n*n
ans[sx*n+sy] = 0
dp[M][sx*n+sy] = 0
hlis = [deque() for i in range(M+1)]
hlis[M].append([0,sx*n+sy])
for life in range(M+1)[::-1]:
    h = hlis[life]
    while h:
        d,ind = h.popleft()
        x,y = divmod(ind,n)
        if dp[life][ind] != d:
            continue

        for (i,j) in ((1,0),(-1,0),(0,1),(0,-1)):
            nx = x+i
            ny = y+j
            if 0 <= nx < n and 0 <= ny < n:
                nind = nx*n+ny
                if life > L[nx][ny] and dp[life-L[nx][ny]][nind] > d+1 and ans[nind] > d+1:
                    dp[life-L[nx][ny]][nind] = d+1
                    hlis[life-L[nx][ny]].append([d+1,nind])
                    ans[nind] = d+1

ans = ans[gx*n+gy]
if ans == inf:
    ans = -1
print(ans)
0