結果

問題 No.13 囲みたい!
ユーザー 👑 obakyanobakyan
提出日時 2019-06-12 23:28:13
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 1,738 bytes
コンパイル時間 39 ms
コンパイル使用メモリ 6,816 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-20 05:42:35
合計ジャッジ時間 850 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 5 ms
6,944 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 6 ms
6,944 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 6 ms
6,940 KB
testcase_08 AC 5 ms
6,944 KB
testcase_09 AC 4 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local w, h = io.read("*n", "*n", "*l")
local str = ""
local col = {}
local done = {}
for i = 1, h do
  str = io.read()
  local idx = (i - 1) * w
  for v in str:gmatch("%d+") do
    idx = idx + 1
    col[idx] = tonumber(v)
    done[idx] = false
  end
end

local function search(startpos)
  local tasks = {startpos}
  local tasknum, donenum = 1, 0
  while donenum < tasknum do
    donenum = donenum + 1
    local idx = tasks[donenum]
    done[idx] = true
    local known = 0
    if w ~= 1 then
      if idx % w ~= 0 and col[idx + 1] == col[idx] then
        if done[idx + 1] then
          known = known + 1
        else
          table.insert(tasks, idx + 1)
          tasknum = tasknum + 1
        end
      end
      if idx % w ~= 1 and col[idx - 1] == col[idx] then
        if done[idx - 1] then
          known = known + 1
        else
          table.insert(tasks, idx - 1)
          tasknum =tasknum + 1
        end
      end
    end
    if h ~= 1 then
      if idx <= (h - 1) * w and col[idx + w] == col[idx] then
        if done[idx + w] then
          known = known + 1
        else
          table.insert(tasks, idx + w)
          tasknum = tasknum + 1
        end
      end
      if w < idx and col[idx - w] == col[idx] then
        if done[idx - w] then
          known = known + 1
        else
          table.insert(tasks, idx - w)
          tasknum =tasknum + 1
        end
      end
    end
    if 2 <= known then
      return true
    end
  end
  return false
end

local curpos = 1
local found = false
while curpos <= h * w do
  found = search(curpos)
  if found then break end
  curpos = curpos + 1
  while curpos <= h * w and done[curpos] do
    curpos = curpos + 1
  end
end
print(found and "possible" or "impossible")
0