結果
問題 | No.124 門松列(3) |
ユーザー |
👑 |
提出日時 | 2021-06-14 21:04:07 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
AC
|
実行時間 | 10 ms / 5,000 ms |
コード長 | 1,464 bytes |
コンパイル時間 | 220 ms |
コンパイル使用メモリ | 6,944 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-24 21:45:13 |
合計ジャッジ時間 | 1,268 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
local mfl, mce = math.floor, math.ceil local w, h = io.read("*n", "*n") --LDUR local n = w * h local function kado(a, b, c) if a == c then return false end return 0 < (b - a) * (b - c) end local t = {} for i = 1, h do for j = 1, w do local idx = (i - 1) * w + j t[idx] = io.read("*n") end end local inf = n * 4 + 100 local len = {} for i = 1, n * 4 do len[i] = inf end local tasks = {} if t[1] ~= t[2] then len[n * 3 + 2] = 1 table.insert(tasks, n * 3 + 2) end if t[1] ~= t[1 + w] then len[n + 1 + w] = 1 table.insert(tasks, n + 1 + w) end local function walk(a, b, rawdst, dst, l) if kado(a, b, t[dst]) and len[rawdst] == inf then len[rawdst] = l table.insert(tasks, rawdst) end end local done = 0 while done < #tasks do done = done + 1 local rawsrc = tasks[done] local way = mce(rawsrc / n) local src = rawsrc - (way - 1) * n local row = mce(src / w) local col = src - (row - 1) * w local b = t[src] local a = 0 if way == 1 then a = t[src + 1] elseif way == 2 then a = t[src - w] elseif way == 3 then a = t[src + w] else a = t[src - 1] end local l = len[rawsrc] + 1 if 1 < col then walk(a, b, src - 1, src - 1, l) end if col < w then walk(a, b, src + 1 + n * 3, src + 1, l) end if 1 < row then walk(a, b, src - w + n * 2, src - w, l) end if row < h then walk(a, b, src + w + n, src + w, l) end end local v = math.min(len[2 * n], len[4 * n]) print(v == inf and -1 or v)