結果

問題 No.34 砂漠の行商人
ユーザー 👑 obakyan
提出日時 2019-06-08 17:05:29
言語 Lua
(LuaJit 2.1.1734355927)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,327 bytes
コンパイル時間 65 ms
コンパイル使用メモリ 6,948 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-05 23:42:45
合計ジャッジ時間 1,222 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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