結果

問題 No.8063 幅優先探索
ユーザー siman
提出日時 2021-02-24 07:03:30
言語 Ruby
(3.4.1)
結果
AC  
実行時間 1,979 ms / 2,000 ms
コード長 564 bytes
コンパイル時間 237 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 86,528 KB
最終ジャッジ日時 2024-09-23 06:00:14
合計ジャッジ時間 5,037 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 9
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

R, C = gets.split.map(&:to_i)
SY, SX = gets.split.map(&:to_i)
GY, GX = gets.split.map(&:to_i)
G = R.times.map { gets.chomp.chars }
DY = [-1, 0, 1, 0]
DX = [0, 1, 0, -1]

queue = []
queue << [SY - 1, SX - 1, 0]
visited = Array.new(R) { Array.new(C, false) }

until queue.empty?
  y, x, dist = queue.shift

  if y == GY - 1 && x == GX - 1
    puts dist
    exit
  end

  next if visited[y][x]
  visited[y][x] = true

  4.times do |i|
    ny = y + DY[i]
    nx = x + DX[i]
    next if ny < 0 || nx < 0 || R <= ny || C <= nx

    queue << [ny, nx, dist + 1]
  end
end
0