結果

問題 No.697 池の数はいくつか
ユーザー 👑 obakyanobakyan
提出日時 2019-05-12 13:35:57
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 3,814 ms / 6,000 ms
コード長 2,299 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 6,940 KB
実行使用メモリ 19,616 KB
最終ジャッジ日時 2024-04-25 20:28:41
合計ジャッジ時間 22,671 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 301 ms
6,944 KB
testcase_25 AC 310 ms
6,940 KB
testcase_26 AC 342 ms
6,944 KB
testcase_27 AC 324 ms
6,940 KB
testcase_28 AC 325 ms
6,940 KB
testcase_29 AC 3,814 ms
11,068 KB
testcase_30 AC 2,789 ms
19,536 KB
testcase_31 AC 3,724 ms
11,052 KB
testcase_32 AC 2,857 ms
19,588 KB
testcase_33 AC 2,947 ms
19,544 KB
testcase_34 AC 2,882 ms
19,616 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 mapbox = {}
for i = 1, math.ceil(h * w / 16) do
  mapbox[i] = 0
end
local function setval(idx, val)
  local boxidx = math.ceil(idx / 16)
  local boxval = mapbox[boxidx]
  local newval, mul = 0, 1
  local boxpos = idx % 16
  for i = 0, 15 do
    if(i == boxpos) then
      newval = newval + mul * (val % 2)
    else
      newval = newval + mul * (boxval % 2)
    end
    boxval = math.floor(boxval / 2)
    mul = mul * 2
  end
  mapbox[boxidx] = newval
end
local function getval(idx)
  local boxidx = math.ceil(idx / 16)
  local boxval = mapbox[boxidx]
  local boxpos = idx % 16
  for i = 1, boxpos do boxval = math.floor(boxval / 2) end
  return boxval % 2
end
local str = ""
for i_h = 1, h do
  str = ior:read()
  for i_w = 1, w do
    local idx = getindex(i_h, i_w)
    if(str:sub(i_w * 2 - 1, i_w * 2 - 1) == "1") then
      setval(idx, 1)
    else
      setval(idx, 0)
    end
  end
end

-- for i_h = 1, h do
--   for i_w = 1, w do
--     if(i_w ~= 1) then io.write(" ") end
--     local idx = getindex(i_h, i_w)
--     local a = getval(idx)
--     io.write(a)
--   end
--   io.write("\n")
-- end
-- os.exit()

local tasks = {}
local tasknum = 0
local done = 0
local tasklim = h + w

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

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