結果

問題 No.157 2つの空洞
ユーザー 👑 obakyanobakyan
提出日時 2019-04-30 11:04:20
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,824 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 5,296 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 12:38:33
合計ジャッジ時間 1,287 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local ior = io.input()
local w, h = ior:read("*n", "*n", "*l")
local sh, sw, gh, gw = ior:read("*n", "*n", "*n", "*n", "*l")
local str = ""
local map = {}
local function getindex(xh, xw)
  return (xh - 1) * w + xw
end
local cue = {}
local tasks, done = 1, 0

local bcue = {}
local btasks, bdone = 0, 0

local startfound = false
for i = 1, h do
  str = ior:read()
  for j = 1, w do
    local idx = getindex(i, j)
    if(str:sub(j, j) == ".") then
      if(not startfound) then
        table.insert(cue, idx)
        map[idx] = 0
        startfound = true
      else
        map[idx] = -2
      end
    else
      map[idx] = -1
    end
  end
end

local ret_cand = 40

local function walk(src, dst)
  if (map[dst] == -2) then
    map[dst] = 0
    tasks = tasks + 1
    table.insert(cue, dst)
  elseif(map[dst] == -1) then
    map[dst] = 1
    btasks = btasks + 1
    table.insert(bcue, dst)
  end
end

local function smash(src, dst)
  if (map[dst] == -1) then
    map[dst] = map[src] + 1
    btasks = btasks + 1
    table.insert(bcue, dst)
  elseif(0 < map[dst]) then
    if(map[src] + 1 < map[dst]) then
      map[dst] = map[src] + 1
      btasks = btasks + 1
      table.insert(bcue, dst)
    end
  elseif(map[dst] == -2) then
    ret_cand = math.min(ret_cand, map[src])
  end
end

while(done < tasks) do
  done = done + 1
  local idx = cue[done]
  if(w < idx) then walk(idx, idx - w) end
  if(idx <= (h - 1) * w) then walk(idx, idx + w) end
  if(idx % w ~= 0) then walk(idx, idx + 1) end
  if(idx % w ~= 1) then walk(idx, idx - 1) end
end

while(bdone < btasks) do
  bdone = bdone + 1
  local idx = bcue[bdone]
  if(w < idx) then smash(idx, idx - w) end
  if(idx <= (h - 1) * w) then smash(idx, idx + w) end
  if(idx % w ~= 0) then smash(idx, idx + 1) end
  if(idx % w ~= 1) then smash(idx, idx - 1) end
end
print(ret_cand)
0