結果

問題 No.659 徘徊迷路
ユーザー 👑 obakyanobakyan
提出日時 2021-04-19 21:50:12
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 1,905 bytes
コンパイル時間 124 ms
コンパイル使用メモリ 5,220 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-17 08:27:05
合計ジャッジ時間 1,611 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 6 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 7 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 9 ms
4,380 KB
testcase_09 AC 20 ms
4,380 KB
testcase_10 AC 21 ms
4,376 KB
testcase_11 AC 18 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 21 ms
4,376 KB
testcase_14 AC 20 ms
4,380 KB
testcase_15 AC 19 ms
4,380 KB
testcase_16 AC 19 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl = math.floor
local h, w, tm = io.read("*n", "*n", "*n")
local sr, sc = io.read("*n", "*n")
local gr, gc = io.read("*n", "*n", "*l")
local t = {}
_u = io.read()
for i = 2, h - 1 do
  local s = io.read()
  for j = 2, w - 1 do
    local idx = (i - 2) * (w - 2) + (j - 1)
    t[idx] = s:sub(j, j) == "."
  end
end
_u = io.read()
h = h - 2
w = w - 2
local n = h * w
local sidx = (sr - 1) * w + sc
local gidx = (gr - 1) * w + gc
local mat = {}
for i = 1, n do
  mat[i] = {}
  for j = 1, n do
    mat[i][j] = 0
  end
end
for i = 1, h do for j = 1, w do
  local idx = (i - 1) * w + j
  if t[idx] then
    local c = 0
    if 1 < i and t[idx - w] then c = c + 1 end
    if i < h and t[idx + w] then c = c + 1 end
    if 1 < j and t[idx - 1] then c = c + 1 end
    if j < w and t[idx + 1] then c = c + 1 end
    if 0 < c then
      if 1 < i and t[idx - w] then mat[idx][idx - w] = 1 / c end
      if i < h and t[idx + w] then mat[idx][idx + w] = 1 / c end
      if 1 < j and t[idx - 1] then mat[idx][idx - 1] = 1 / c end
      if j < w and t[idx + 1] then mat[idx][idx + 1] = 1 / c end
    else
      mat[idx][idx] = 1
    end
  end
end end
local remat = {}
for i = 1, n do
  remat[i] = {}
  for j = 1, n do
    remat[i][j] = 0
  end
end
local function matmat()
  for i = 1, n do
    for j = 1, n do
      local v = 0
      for k = 1, n do
        v = v + mat[i][k] * mat[k][j]
      end
      remat[i][j] = v
    end
  end
  for i = 1, n do for j = 1, n do
    mat[i][j] = remat[i][j]
  end end
end
local vec, revec = {}, {}
for i = 1, n do
  vec[i] = 0
  revec[i] = 0
end
local function vecmat()
  for i = 1, n do
    local v = 0
    for k = 1, n do
      v = v + vec[k] * mat[k][i]
    end
    revec[i] = v
  end
  for i = 1, n do vec[i] = revec[i] end
end
vec[sidx] = 1
while 0 < tm do
  if tm % 2 == 1 then
    vecmat()
  end
  matmat()
  tm = mfl(tm / 2)
end
print(string.format("%.10f", vec[gidx]))
0