結果

問題 No.34 砂漠の行商人
ユーザー dangodango
提出日時 2023-07-09 15:57:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 205 ms / 5,000 ms
コード長 1,301 bytes
コンパイル時間 1,072 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 82,808 KB
最終ジャッジ日時 2023-09-30 19:18:43
合計ジャッジ時間 7,718 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 164 ms
79,944 KB
testcase_01 AC 166 ms
79,880 KB
testcase_02 AC 186 ms
81,820 KB
testcase_03 AC 174 ms
80,532 KB
testcase_04 AC 193 ms
82,140 KB
testcase_05 AC 193 ms
82,264 KB
testcase_06 AC 192 ms
82,368 KB
testcase_07 AC 198 ms
82,712 KB
testcase_08 AC 202 ms
82,808 KB
testcase_09 AC 197 ms
82,780 KB
testcase_10 AC 194 ms
82,368 KB
testcase_11 AC 196 ms
82,692 KB
testcase_12 AC 194 ms
82,184 KB
testcase_13 AC 203 ms
82,544 KB
testcase_14 AC 205 ms
82,276 KB
testcase_15 AC 183 ms
82,196 KB
testcase_16 AC 192 ms
82,708 KB
testcase_17 AC 183 ms
82,336 KB
testcase_18 AC 183 ms
82,236 KB
testcase_19 AC 196 ms
82,712 KB
testcase_20 AC 196 ms
82,660 KB
testcase_21 AC 188 ms
82,740 KB
testcase_22 AC 188 ms
82,504 KB
testcase_23 AC 184 ms
82,324 KB
testcase_24 AC 202 ms
82,324 KB
testcase_25 AC 195 ms
82,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List, Tuple

def main():
    rd = input().split()
    n, v = int(rd[0]), int(rd[1])
    sx, sy = int(rd[2]) - 1, int(rd[3]) - 1
    gx, gy = int(rd[4]) - 1, int(rd[5]) - 1
    lij = [list(map(int, input().split())) for _ in range(n)]

    eij: List[List[Tuple[int, int]]] = [[] for _ in range(n * n)]
    for j in range(n):
        for i in range(n):
            p = (j, i)
            for sib in sibPoints:
                np = (p[0] + sib[0], p[1] + sib[1])
                if np[0] >= 0 and np[1] >= 0 and np[0] < n and np[1] < n:
                    eij[p[0] + p[1] * n].append((np[0] + np[1] * n, lij[np[1]][np[0]]))

    s = sx + sy * n
    g = gx + gy * n

    def calc():
        qi = [s]
        dp = [-1] * (n * n)
        dp[s] = 0

        r = 1
        while len(qi) > 0:
            ri = []
            for q in qi:
                for e in eij[q]:
                    nv = dp[q] + e[1]
                    if nv < v and (dp[e[0]] < 0 or dp[e[0]] > nv):
                        if e[0] == g:
                            return r
                        dp[e[0]] = nv
                        ri.append(e[0])
            qi = ri
            r += 1

        return -1

    print(calc())

sibPoints = [(-1, 0), (0, -1), (1, 0), (0, 1)]
if __name__ == '__main__':
  main()
0