結果

問題 No.402 最も海から遠い場所
ユーザー 👑 obakyan
提出日時 2019-07-19 23:18:49
言語 Lua
(LuaJit 2.1.1734355927)
結果
AC  
実行時間 1,989 ms / 3,000 ms
コード長 1,856 bytes
コンパイル時間 582 ms
コンパイル使用メモリ 5,504 KB
実行使用メモリ 403,968 KB
最終ジャッジ日時 2024-12-26 04:04:12
合計ジャッジ時間 9,315 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

local ior = io.input()
local h, w = ior:read("*n", "*n", "*l")

local function getindex(i_h, i_w)
  return i_w + (i_h - 1) * w
end

local taskstate = {}
for i = 1, h * w do taskstate[i] = false end
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = h * w

local function addtask(idx)
  if(not taskstate[idx]) then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
  end
end

local function createMap()
  local str = ""
  local map = {}
  local inf = h + w
  for i_h = 1, h do
    str = ior:read()
    for i_w = 1, w do
      local index = getindex(i_h, i_w)
      if str:sub(i_w, i_w) == "#" then
        if i_h == 1 or i_h == h or i_w == 1 or i_w == w then
          map[index] = 1
          addtask(index)
        else
          map[index] = inf
        end
      else
        map[index] = 0
        addtask(index)
      end
    end
  end
  return map
end

local map = createMap()

local function walk(src, dst)
  if map[src] + 1 < map[dst] then
    map[dst] = map[src] + 1
    addtask(dst)
  end
end

while(done < tasknum) do
  done = done + 1
  local taskidx = done % tasklim
  if(taskidx == 0) then taskidx = tasklim end
  local idx = tasks[taskidx]
  taskstate[idx] = false

  if w < idx then walk(idx, idx - w) end
  if idx <= (h - 1) * w then walk(idx, idx + w) end
  if 1 < w then
    if idx % w ~= 0 then
      walk(idx, idx + 1)
      if w < idx then walk(idx, idx + 1 - w) end
      if idx <= (h - 1) * w then walk(idx, idx + 1 + w) end
    end
    if idx % w ~= 1 then
      walk(idx, idx - 1)
      if w < idx then walk(idx, idx - 1 - w) end
      if idx <= (h - 1) * w then walk(idx, idx - 1 + w) end
    end
  end
end
local mma = math.max
local ret = 1
for i = 1, h * w do
  ret = mma(ret, map[i])
end
print(ret)
0