結果

問題 No.124 門松列(3)
ユーザー 👑 obakyanobakyan
提出日時 2021-06-14 21:04:07
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 1,464 bytes
コンパイル時間 216 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 06:33:04
合計ジャッジ時間 1,900 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 7 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 6 ms
4,376 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 7 ms
4,376 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 4 ms
4,380 KB
testcase_29 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0