結果

問題 No.2411 Reverse Directions
ユーザー 👑 obakyanobakyan
提出日時 2023-08-11 22:19:39
言語 Lua
(LuaJit 2.1.1696795921)
結果
WA  
実行時間 -
コード長 2,204 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 5,404 KB
実行使用メモリ 9,708 KB
最終ジャッジ日時 2023-08-11 22:19:47
合計ジャッジ時間 4,771 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

--[[
assert for
3 3 8 2 5
..#
...
#..
]]

local mfl, mce = math.floor, math.ceil
local h, w = io.read("*n", "*n")
local k, l, r = io.read("*n", "*n", "*n", "*l")
local t = {}
local len = {}
for i = 1, h do
  t[i] = {}
  len[i] = {}
  local s = io.read()
  for j = 1, w do
    t[i][j] = s:sub(j, j) == "#"
    len[i][j] = -999
  end
end
local minlen = h + w - 2
if (minlen + k) % 2 == 1 then
  print("No") os.exit()
end
if (r - l) % 2 == 0 then
  print("No") os.exit()
end
if l == 1 then
  print("No") os.exit()
end
len[1][1] = 0
local tasks = {1}
local done = 0

local function walk(dst, dr, dc, l)
  if not t[dr][dc] and len[dr][dc] < 0 then
    len[dr][dc] = l
    table.insert(tasks, dst)
  end
end

while done < #tasks do
  done = done + 1
  local src = tasks[done]
  local row = mce(src / w)
  local col = src - (row - 1) * w
  local l2 = len[row][col] + 1
  if 1 < row then
    walk(src - w, row - 1, col, l2)
  end
  if row < h then
    walk(src + w, row + 1, col, l2)
  end
  if 1 < col then
    walk(src - 1, row, col - 1, l2)
  end
  if col < w then
    walk(src + 1, row, col + 1, l2)
  end
end
if len[h][w] < 0 or k - (r - l + 1) < len[h][w] then
  print("No") os.exit()
end
print("Yes")
local ans = {}
local ph, pw = h, w
while 1 < ph or 1 < pw do
  if 1 < ph and len[ph - 1][pw] + 1 == len[ph][pw] then
    table.insert(ans, "D")
    ph = ph - 1
  elseif ph < h and len[ph + 1][pw] + 1 == len[ph][pw] then
    table.insert(ans, "U")
    ph = ph + 1
  elseif 1 < pw and len[ph][pw - 1] + 1 == len[ph][pw] then
    table.insert(ans, "R")
    pw = pw - 1
  else
    table.insert(ans, "L")
    pw = pw + 1
  end
end
ans = table.concat(ans):reverse()
local rep = mfl((k - len[h][w]) / 2)
if l == 1 then
  assert(false)
  -- if ans:sub(1, 1) == "R" then
    -- print(string.rep("RL", rep) .. ans)
  -- else
    -- print(string.rep("DU", rep) .. ans)
  -- end
else
  io.write(ans:sub(1, l))
  local b = ans:sub(l, l)
  if b == "R" then
    io.write(string.rep("LR", rep))
  elseif b == "L" then
    io.write(string.rep("RL", rep))
  elseif b == "D" then
    io.write(string.rep("UD", rep))
  else
    io.write(string.rep("DU", rep))
  end
  io.write(ans:sub(l + 1, #ans) .. "\n")
  io.flush()
end
0