結果
| 問題 |
No.13 囲みたい!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-06-12 23:28:13 |
| 言語 | Lua (LuaJit 2.1.1734355927) |
| 結果 |
AC
|
| 実行時間 | 6 ms / 5,000 ms |
| コード長 | 1,738 bytes |
| コンパイル時間 | 275 ms |
| コンパイル使用メモリ | 5,248 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-12 00:44:11 |
| 合計ジャッジ時間 | 995 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 16 |
ソースコード
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")