# AtCoder Beginner Contest 007

URL: https://atcoder.jp/contests/abc007 (opens new window)

# C

r,c = gets.chomp.split(" ").map(&:to_i)
sy,sx = gets.chomp.split(" ").map(&:to_i)
gy,gx = gets.chomp.split(" ").map(&:to_i)
arr = r.times.map{gets.chomp}

# 座標調整
sy -= 1
sx -= 1
gy -= 1
gx -= 1

# 座標, 回数
bfs = [[sy, sx, 0]]
arr[sy][sx] = "#"
y, x, result = nil, nil, nil
while bfs.length > 0 do
  y, x, result = bfs.shift
  break if y == gy && x == gx
  [[-1, 0], [1, 0], [0, 1], [0, -1]].each do |(dy, dx)|
    tmp_y = y + dy
    tmp_x = x + dx
    next if arr[tmp_y][tmp_x] == "#"
    arr[tmp_y][tmp_x] = "#"
    bfs << [tmp_y, tmp_x, result + 1]
  end
end

puts result 
Last Updated: 2020/08/15 20:23