結果

問題 No.697 池の数はいくつか
ユーザー 👑 obakyanobakyan
提出日時 2019-05-12 11:50:18
言語 Lua
(LuaJit 2.1.1696795921)
結果
MLE  
実行時間 -
コード長 1,528 bytes
コンパイル時間 247 ms
コンパイル使用メモリ 7,068 KB
実行使用メモリ 395,896 KB
最終ジャッジ日時 2024-05-04 06:27:08
合計ジャッジ時間 15,602 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 217 ms
23,256 KB
testcase_25 AC 219 ms
23,316 KB
testcase_26 AC 212 ms
23,272 KB
testcase_27 AC 211 ms
23,264 KB
testcase_28 AC 221 ms
23,276 KB
testcase_29 MLE -
testcase_30 MLE -
testcase_31 MLE -
testcase_32 MLE -
testcase_33 MLE -
testcase_34 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

local function createMap()
  local map = {}
  for i_h = 1, h do
    for i_w = 1, w do
      local idx = getindex(i_h, i_w)
      map[idx] = ior:read("*n")
    end
  end
  return map
end

local map = createMap()

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 curpos = 1
local lakecount = 0
local function searchtask()
  for i = curpos, h * w do
    if(map[i] == 1) then
      lakecount = lakecount + 1
      curpos = i
      addtask(i)
      break
    end
  end
end
searchtask()
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
  map[idx] = 2
  if(w < idx) then if(map[idx - w] == 1) then addtask(idx - w) end end
  if(idx <= (h - 1) * w) then if(map[idx + w] == 1) then addtask(idx + w) end end
  if(1 < w) then
    if(idx % w ~= 0) then if(map[idx + 1] == 1) then addtask(idx + 1) end end
    if(idx % w ~= 1) then if(map[idx - 1] == 1) then addtask(idx - 1) end end
  end
  if(done == tasknum) then
    searchtask()
  end
end
print(lakecount)
0