結果

問題 No.340 雪の足跡
ユーザー 👑 obakyan
提出日時 2020-04-25 23:38:23
言語 Lua
(LuaJit 2.1.1734355927)
結果
AC  
実行時間 507 ms / 1,000 ms
コード長 2,578 bytes
コンパイル時間 345 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 44,544 KB
最終ジャッジ日時 2024-11-08 02:14:40
合計ジャッジ時間 8,794 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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])
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0