結果

問題 No.402 最も海から遠い場所
ユーザー 👑 obakyanobakyan
提出日時 2019-07-19 23:18:49
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 2,096 ms / 3,000 ms
コード長 1,856 bytes
コンパイル時間 343 ms
コンパイル使用メモリ 5,308 KB
実行使用メモリ 400,112 KB
最終ジャッジ日時 2023-08-27 00:04:47
合計ジャッジ時間 9,443 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 13 ms
4,384 KB
testcase_15 AC 35 ms
8,700 KB
testcase_16 AC 61 ms
15,236 KB
testcase_17 AC 738 ms
202,780 KB
testcase_18 AC 2,096 ms
400,112 KB
testcase_19 AC 948 ms
395,576 KB
testcase_20 AC 1,137 ms
395,636 KB
testcase_21 AC 987 ms
395,696 KB
権限があれば一括ダウンロードができます

ソースコード

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