結果
| 問題 | No.697 池の数はいくつか |
| ユーザー |
|
| 提出日時 | 2019-05-12 19:31:24 |
| 言語 | Lua (LuaJit 2.1.1765228720) |
| 結果 |
AC
|
| 実行時間 | 1,117 ms / 6,000 ms |
| コード長 | 1,860 bytes |
| 記録 | |
| コンパイル時間 | 267 ms |
| コンパイル使用メモリ | 5,248 KB |
| 実行使用メモリ | 86,272 KB |
| 最終ジャッジ日時 | 2024-11-08 08:16:30 |
| 合計ジャッジ時間 | 9,527 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
local mce, mfl = math.ceil, math.floor
local tnu = tonumber
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, mce(h * w / 2) do
mapbox[i] = 0
end
local function setval(idx, val)
local boxidx = mce(idx / 2)
local boxval = mapbox[boxidx]
if(idx % 2 == 0) then
mapbox[boxidx] = 2 * mfl(boxval / 2) + val
else
mapbox[boxidx] = 2 * val + (boxval % 2)
end
end
local function getval(idx)
local boxval = mapbox[mce(idx / 2)]
if(idx % 2 == 0) then
return boxval % 2
else
return mfl(boxval / 2)
end
end
local str = ""
local tidx = 1
for i_h = 1, h do
str = ior:read()
for i_w = 1, w do
setval(tidx, tnu(str:sub(i_w * 2 - 1, i_w * 2 - 1)))
tidx = tidx + 1
end
end
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)