結果

問題 No.340 雪の足跡
ユーザー 👑 obakyanobakyan
提出日時 2020-04-25 23:38:23
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 493 ms / 1,000 ms
コード長 2,578 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 6,816 KB
実行使用メモリ 44,592 KB
最終ジャッジ日時 2024-04-25 15:14:02
合計ジャッジ時間 8,314 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 7 ms
6,940 KB
testcase_12 AC 6 ms
6,944 KB
testcase_13 AC 6 ms
6,944 KB
testcase_14 AC 30 ms
7,680 KB
testcase_15 AC 34 ms
8,320 KB
testcase_16 AC 24 ms
6,940 KB
testcase_17 AC 41 ms
8,448 KB
testcase_18 AC 38 ms
8,448 KB
testcase_19 AC 45 ms
8,448 KB
testcase_20 AC 301 ms
38,308 KB
testcase_21 AC 192 ms
6,940 KB
testcase_22 AC 42 ms
36,224 KB
testcase_23 AC 374 ms
44,420 KB
testcase_24 AC 278 ms
36,280 KB
testcase_25 AC 287 ms
37,280 KB
testcase_26 AC 48 ms
6,944 KB
testcase_27 AC 15 ms
6,944 KB
testcase_28 AC 46 ms
6,940 KB
testcase_29 AC 396 ms
40,848 KB
testcase_30 AC 267 ms
26,752 KB
testcase_31 AC 416 ms
43,616 KB
testcase_32 AC 493 ms
44,444 KB
testcase_33 AC 390 ms
44,548 KB
testcase_34 AC 490 ms
44,592 KB
testcase_35 AC 358 ms
44,448 KB
testcase_36 AC 349 ms
44,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local w, h, nq = io.read("*n", "*n", "*n")
local rows = {}
for i = 1, h do
  rows[i] = {}
  for j = 1, w do
    rows[i][j] = 0
  end
end
local cols = {}
for i = 1, w do
  cols[i] = {}
  for j = 1, h do
    cols[i][j] = 0
  end
end

local function getrowcol(idx)
  local c = idx % w + 1
  local r = mfl(idx / w) + 1
  return r, c
end

for i = 1, nq do
  local m = io.read("*n")
  local src = io.read("*n")
  local src_row, src_col = getrowcol(src)
  for j = 1, m do
    local dst = io.read("*n")
    local dst_row, dst_col = getrowcol(dst)
    if src_row == dst_row then
      if src_col < dst_col then
        rows[src_row][src_col] = rows[src_row][src_col] + 1
        rows[src_row][dst_col] = rows[src_row][dst_col] - 1
      else
        rows[src_row][src_col] = rows[src_row][src_col] - 1
        rows[src_row][dst_col] = rows[src_row][dst_col] + 1
      end
    else
      if src_row < dst_row then
        cols[src_col][src_row] = cols[src_col][src_row] + 1
        cols[src_col][dst_row] = cols[src_col][dst_row] - 1
      else
        cols[src_col][src_row] = cols[src_col][src_row] - 1
        cols[src_col][dst_row] = cols[src_col][dst_row] + 1
      end
    end
    src_row, src_col = dst_row, dst_col
  end
end
for i = 1, h do
  for j = 2, w do
    rows[i][j] = rows[i][j] + rows[i][j - 1]
  end
end
for i = 1, w do
  for j = 2, h do
    cols[i][j] = cols[i][j] + cols[i][j - 1]
  end
end

local inf = 1000000007
local len = {}
local taskstate = {}
for i = 1, h * w do
  taskstate[i] = false
  len[i] = inf
end
len[1] = 0
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = h * w

local function addtask(idx)
  if not taskstate[idx] then
    taskstate[idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = idx
  end
end

local function walk(src, dst)
  if len[src] + 1 < len[dst] then
    len[dst] = len[src] + 1
    addtask(dst)
  end
end

addtask(1)

while done < tasknum do
  done = done + 1
  local taskidx = done % tasklim
  if taskidx == 0 then taskidx = tasklim end
  local idx = tasks[taskidx]
  taskstate[idx] = false
  local r, c = getrowcol(idx - 1)

  if w < idx and 0 < cols[c][r - 1] then walk(idx, idx - w) end
  if idx <= (h - 1) * w and 0 < cols[c][r] then walk(idx, idx + w) end
  if 1 < w then
    if idx % w ~= 0 and 0 < rows[r][c] then walk(idx, idx + 1) end
    if idx % w ~= 1 and 0 < rows[r][c - 1] then walk(idx, idx - 1) end
  end
end
print(inf <= len[h * w] and "Odekakedekinai.." or len[h * w])
0