結果
問題 | No.340 雪の足跡 |
ユーザー |
👑 |
提出日時 | 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 |
ソースコード
local mfl, mce = math.floor, math.ceillocal w, h, nq = io.read("*n", "*n", "*n")local rows = {}for i = 1, h dorows[i] = {}for j = 1, w dorows[i][j] = 0endendlocal cols = {}for i = 1, w docols[i] = {}for j = 1, h docols[i][j] = 0endendlocal function getrowcol(idx)local c = idx % w + 1local r = mfl(idx / w) + 1return r, cendfor i = 1, nq dolocal m = io.read("*n")local src = io.read("*n")local src_row, src_col = getrowcol(src)for j = 1, m dolocal dst = io.read("*n")local dst_row, dst_col = getrowcol(dst)if src_row == dst_row thenif src_col < dst_col thenrows[src_row][src_col] = rows[src_row][src_col] + 1rows[src_row][dst_col] = rows[src_row][dst_col] - 1elserows[src_row][src_col] = rows[src_row][src_col] - 1rows[src_row][dst_col] = rows[src_row][dst_col] + 1endelseif src_row < dst_row thencols[src_col][src_row] = cols[src_col][src_row] + 1cols[src_col][dst_row] = cols[src_col][dst_row] - 1elsecols[src_col][src_row] = cols[src_col][src_row] - 1cols[src_col][dst_row] = cols[src_col][dst_row] + 1endendsrc_row, src_col = dst_row, dst_colendendfor i = 1, h dofor j = 2, w dorows[i][j] = rows[i][j] + rows[i][j - 1]endendfor i = 1, w dofor j = 2, h docols[i][j] = cols[i][j] + cols[i][j - 1]endendlocal inf = 1000000007local len = {}local taskstate = {}for i = 1, h * w dotaskstate[i] = falselen[i] = infendlen[1] = 0local tasks = {}local tasknum = 0local done = 0local tasklim = h * wlocal function addtask(idx)if not taskstate[idx] thentaskstate[idx] = truetasknum = tasknum + 1local taskidx = tasknum % tasklimif taskidx == 0 then taskidx = tasklim endtasks[taskidx] = idxendendlocal function walk(src, dst)if len[src] + 1 < len[dst] thenlen[dst] = len[src] + 1addtask(dst)endendaddtask(1)while done < tasknum dodone = done + 1local taskidx = done % tasklimif taskidx == 0 then taskidx = tasklim endlocal idx = tasks[taskidx]taskstate[idx] = falselocal r, c = getrowcol(idx - 1)if w < idx and 0 < cols[c][r - 1] then walk(idx, idx - w) endif idx <= (h - 1) * w and 0 < cols[c][r] then walk(idx, idx + w) endif 1 < w thenif idx % w ~= 0 and 0 < rows[r][c] then walk(idx, idx + 1) endif idx % w ~= 1 and 0 < rows[r][c - 1] then walk(idx, idx - 1) endendendprint(inf <= len[h * w] and "Odekakedekinai.." or len[h * w])