結果

問題 No.34 砂漠の行商人
ユーザー 👑 obakyanobakyan
提出日時 2019-06-08 17:05:29
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,327 bytes
コンパイル時間 44 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 19:56:56
合計ジャッジ時間 1,050 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 5 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 6 ms
5,376 KB
testcase_07 AC 9 ms
5,376 KB
testcase_08 AC 11 ms
5,376 KB
testcase_09 AC 9 ms
5,376 KB
testcase_10 AC 7 ms
5,376 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 6 ms
5,376 KB
testcase_13 AC 14 ms
5,376 KB
testcase_14 AC 15 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 6 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 6 ms
5,376 KB
testcase_19 AC 10 ms
5,376 KB
testcase_20 AC 13 ms
5,376 KB
testcase_21 AC 6 ms
5,376 KB
testcase_22 AC 6 ms
5,376 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 13 ms
5,376 KB
testcase_25 AC 7 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl = math.floor
local n, v, sx, sy, gx, gy = io.read("*n", "*n", "*n", "*n", "*n", "*n")
local goalidx = (gy - 1) * n + gx
local map = {}
for i = 1, n * n do
  map[i] = io.read("*n")
end
local life = {}
for i = 1, n * n do
  life[i] = 0
end
local n2 = n * n
life[(sy - 1) * n + sx] = v
local alltasks = {}
alltasks[1] = {(sy - 1) * n + sx}
local curtaskidx = 1

local function walk(curidx, dstidx)
  if life[dstidx] < life[curidx] - map[dstidx] then
    if goalidx == dstidx then
      print(curtaskidx)
      return true
    end
    table.insert(alltasks[curtaskidx + 1], dstidx)
    life[dstidx] = life[curidx] - map[dstidx]
  end
  return false
end
local found = false
while 0 < #alltasks[curtaskidx] do
  alltasks[curtaskidx + 1] = {}
  for i = 1, #alltasks[curtaskidx] do
    local idx = alltasks[curtaskidx][i]
    local ret = false
    if idx % n ~= 0 then
      found = walk(idx, idx + 1)
      if found then break end
    end
    if idx % n ~= 1 then
      found = walk(idx, idx - 1)
      if found then break end
    end
    if n < idx then
      found = walk(idx, idx - n)
      if found then break end
    end
    if idx <= n * n - n then
      found = walk(idx, idx + n)
      if found then break end
    end
  end
  if found then break end
  curtaskidx = curtaskidx + 1
end
if not found then print(-1) end
0