# AtCoder Beginner Contest 039

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

# D

h,w = gets.chomp.split(" ").map(&:to_i)
arr = h.times.map{gets.chomp.split("").map{|c| c == "#" ? 1 : 0}}

result = h.times.map{"." * w}

# 外側を黒で埋めたもの
arr2 = []
arr2 << Array.new(w+2, 1)
arr.each do |a|
  arr2 << [1] + a + [1]
end
arr2 << Array.new(w+2, 1)

# コピーを作る
flag = []
arr2.each do |a|
  flag << a.dup
end

1.upto(h) do |i|
  1.upto(w) do |j|
    # 自身と八方向が全部黒か?
    tmp = 
    arr2[i-1][j-1] +
    arr2[i-1][j]   +
    arr2[i-1][j+1] +
    arr2[i][j-1]   +
    arr2[i][j]     +
    arr2[i][j+1]   +
    arr2[i+1][j-1] +
    arr2[i+1][j]   +
    arr2[i+1][j+1] 
    if tmp == 9
      flag[i-1][j-1] = 0
      flag[i-1][j]   = 0
      flag[i-1][j+1] = 0
      flag[i][j-1]   = 0
      flag[i][j]     = 0
      flag[i][j+1]   = 0
      flag[i+1][j-1] = 0
      flag[i+1][j]   = 0
      flag[i+1][j+1] = 0
      result[i-1][j-1] = "#"
    end
  end
end

1.upto(h) do |i|
  1.upto(w) do |j|
    if flag[i][j] == 1
      puts "impossible"
      exit
    end
  end
end

puts "possible"
result.each do |row|
  puts row
end
Last Updated: 2020/08/10 21:46