結果
問題 | 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.ceillocal w, h = io.read("*n", "*n")--LDURlocal n = w * hlocal function kado(a, b, c)if a == c then return false endreturn 0 < (b - a) * (b - c)endlocal t = {}for i = 1, h dofor j = 1, w dolocal idx = (i - 1) * w + jt[idx] = io.read("*n")endendlocal inf = n * 4 + 100local len = {}for i = 1, n * 4 dolen[i] = infendlocal tasks = {}if t[1] ~= t[2] thenlen[n * 3 + 2] = 1table.insert(tasks, n * 3 + 2)endif t[1] ~= t[1 + w] thenlen[n + 1 + w] = 1table.insert(tasks, n + 1 + w)endlocal function walk(a, b, rawdst, dst, l)if kado(a, b, t[dst]) and len[rawdst] == inf thenlen[rawdst] = ltable.insert(tasks, rawdst)endendlocal done = 0while done < #tasks dodone = done + 1local rawsrc = tasks[done]local way = mce(rawsrc / n)local src = rawsrc - (way - 1) * nlocal row = mce(src / w)local col = src - (row - 1) * wlocal b = t[src]local a = 0if way == 1 thena = t[src + 1]elseif way == 2 thena = t[src - w]elseif way == 3 thena = t[src + w]elsea = t[src - 1]endlocal l = len[rawsrc] + 1if 1 < col then walk(a, b, src - 1, src - 1, l) endif col < w then walk(a, b, src + 1 + n * 3, src + 1, l) endif 1 < row then walk(a, b, src - w + n * 2, src - w, l) endif row < h then walk(a, b, src + w + n, src + w, l) endendlocal v = math.min(len[2 * n], len[4 * n])print(v == inf and -1 or v)