結果

問題 No.34 砂漠の行商人
ユーザー rlangevinrlangevin
提出日時 2023-09-19 12:31:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,854 ms / 5,000 ms
コード長 1,232 bytes
コンパイル時間 813 ms
コンパイル使用メモリ 86,948 KB
実行使用メモリ 480,272 KB
最終ジャッジ日時 2023-09-19 12:31:26
合計ジャッジ時間 11,995 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
71,840 KB
testcase_01 AC 102 ms
71,336 KB
testcase_02 AC 127 ms
77,672 KB
testcase_03 AC 98 ms
71,836 KB
testcase_04 AC 213 ms
98,332 KB
testcase_05 AC 215 ms
98,508 KB
testcase_06 AC 124 ms
78,208 KB
testcase_07 AC 296 ms
109,036 KB
testcase_08 AC 344 ms
136,980 KB
testcase_09 AC 103 ms
77,064 KB
testcase_10 AC 103 ms
76,896 KB
testcase_11 AC 102 ms
76,772 KB
testcase_12 AC 170 ms
92,528 KB
testcase_13 AC 110 ms
76,752 KB
testcase_14 AC 104 ms
76,892 KB
testcase_15 AC 104 ms
76,868 KB
testcase_16 AC 104 ms
76,884 KB
testcase_17 AC 103 ms
76,992 KB
testcase_18 AC 98 ms
71,748 KB
testcase_19 AC 2,276 ms
319,536 KB
testcase_20 AC 3,854 ms
480,272 KB
testcase_21 AC 119 ms
78,132 KB
testcase_22 AC 103 ms
76,772 KB
testcase_23 AC 102 ms
76,880 KB
testcase_24 AC 112 ms
76,784 KB
testcase_25 AC 372 ms
135,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
inf = 10 ** 18

N, V, sx, sy, gx, gy = map(int, input().split())
sx, sy, gx, gy = sx-1, sy-1, gx-1, gy-1
d = abs(sx - gx) + abs(sy - gy)
L = []
for i in range(N):
    L.append(list(map(int, input().split())))

if V > d * 9:
    print(d)
    exit()

def f(x, y, v):
    return x + y * N + v * N * N

def g(n):
    v = n // (N * N)
    n %= N * N
    y, x = divmod(n, N)
    return x, y, v 

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
Q = deque([])
dist = defaultdict(lambda:inf)
val = [[-1] * N for i in range(N)]
s =  f(sy, sx, V)
dist[s] = 0
Q.append(s)
x, y, vv = g(s)
val[sy][sx] = V

while Q:
    u = Q.popleft()
    px, py, vv = g(u)
    for k in range(4):
        x = px + dx[k]
        y = py + dy[k]
        if x < 0 or x > N - 1 or y < 0 or y > N - 1:
            continue
        if vv - L[x][y] <= 0:
            continue
        if val[x][y] > vv:
            continue
        v = f(x, y, vv - L[x][y])
        if v in dist:
            continue
        
        dist[v] = dist[u] + 1
        if x == gy and y == gx:
            print(dist[v])
            exit()
        Q.append(v)

ans = inf
for v in range(1, V + 1):
    ans = min(ans, dist[f(gy, gx, v)])

print(ans) if ans != inf else print(-1)
0