結果

問題 No.2412 YOU Grow Bigger!
ユーザー tomeruntomerun
提出日時 2023-08-11 22:46:07
言語 Crystal
(1.11.2)
結果
WA  
実行時間 -
コード長 1,868 bytes
コンパイル時間 14,145 ms
コンパイル使用メモリ 294,912 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-29 13:53:57
合計ジャッジ時間 15,401 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

h, w = read_line.split.map(&.to_i)
s = Array.new(h) { read_line.chars.map { |ch| ch == '#' } }
h.times do |i|
  w.times do |j|
    if s[i][j]
      -2.upto(0) do |dy|
        -2.upto(0) do |dx|
          ny = i + dy
          nx = j + dx
          next if ny < 0 || h <= ny || nx < 0 || w <= nx
          s[ny][nx] = true
        end
      end
    end
  end
end
g = Array.new(h * w + 2) { [] of Tuple(Int32, Int32) }
start = h * w
goal = h * w + 1
3.times do |i|
  1.upto(h - 1) do |j|
    g[start] << {j * w + i, 1}
  end
end
(h - 3).upto(h - 1) do |i|
  0.upto(w - 4) do |j|
    g[start] << {i * w + j, 1}
  end
end
h.times do |i|
  if s[i][0]
    g[start] << {i * w, 0}
  end
  g[i * w + w - 1] << {goal, 0} if i < h - 3
end
w.times do |i|
  if s[h - 1][i]
    g[start] << {(h - 1) * w + i, 0}
  end
  g[i] << {goal, 0} if i > 0
end
h.times do |i|
  w.times do |j|
    if s[i][j]
      if i > 0 && s[i - 1][j]
        g[i * w + j] << {(i - 1) * w + j, 0}
      end
      if i < h - 1 && s[i + 1][j]
        g[i * w + j] << {(i + 1) * w + j, 0}
      end
      if j > 0 && s[i][j - 1]
        g[i * w + j] << {i * w + j - 1, 0}
      end
      if j < w - 1 && s[i][j + 1]
        g[i * w + j] << {i * w + j + 1, 0}
      end
    end
    -3.upto(3) do |dy|
      -3.upto(3) do |dx|
        next if dy.abs + dx.abs == 6
        ny = i + dy
        nx = j + dx
        next if ny < 0 || h <= ny || nx < 0 || w <= nx
        next if ny == 0 && nx == 0
        next if ny >= h - 3 && nx >= w - 3
        g[i * w + j] << {ny * w + nx, 1}
      end
    end
  end
end
dist = Array.new(g.size, 1 << 29)
dist[start] = 0
q = Deque.new([{start, 0}])
while !q.empty?
  cp, cd = q.shift
  g[cp].each do |adj, d|
    nd = cd + d
    next if dist[adj] <= nd
    dist[adj] = nd
    if d == 0
      q.unshift({adj, nd})
    else
      q << {adj, nd}
    end
  end
end
puts dist[goal]
0