Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created May 6, 2019 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/9668d35c21a5297e1965dd06ec64f4b3 to your computer and use it in GitHub Desktop.
Save whatalnk/9668d35c21a5297e1965dd06ec64f4b3 to your computer and use it in GitHub Desktop.
AtCoder AGC #033 A - Darker and Darker [Ruby, TLE]
# TLE
H, W = gets.chomp.split(" ").map(&:to_i)
que = []
visited = Array.new(H){Array.new(W, false)}
H.times do |i|
row = gets.chomp
W.times do |j|
if row[j] == "#"
que << [i, j]
end
end
end
di = [-1, 0, 1, 0]
dj = [0, 1, 0, -1]
ans = 0
que2 = []
while !que.empty?
que2 = []
que.each do |i, j|
visited[i][j] = true
4.times do |d|
ni = i + di[d]
nj = j + dj[d]
if ni >= 0 && ni < H && nj >= 0 && nj < W && !visited[ni][nj]
que2 << [ni, nj]
end
end
end
que = que2
ans += 1
end
puts ans - 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment