結果

問題 No.179 塗り分け
ユーザー snipsnipsnip
提出日時 2015-04-06 01:13:47
言語 Ruby
(3.4.1)
結果
WA  
実行時間 -
コード長 925 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 77,764 KB
最終ジャッジ日時 2024-10-02 13:42:21
合計ジャッジ時間 22,134 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5 WA * 1
other AC * 30 WA * 8 RE * 2
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

require 'set'

H, W = gets.split.map(&:to_i)

class << MAP = Array.new(H) { gets.strip }
  def ok?(dx, dy)
    d = dy * W + dx
    bs = blacks - blacks.map {|i| i + d if i % W + dx < W }
    bs.size == blacks.size / 2 && bs.all? {|i| flat[i + d] == 35 }
  end

  def solve
    ax, ay, bx, by = aabb
    (1..by - ay).each do |dy|
      return :YES if ok?(0, dy)
    end
    (1..bx - ax).each do |dx|
      (0..by - ay).each do |dy|
        return :YES if ok?(dx, dy)
      end
    end
    :NO
  end

  def flat
    @flat ||= map(&:bytes).flatten
  end

  def blacks
    @blacks ||= Set.new(flat.each_with_index.map {|cell, i| i if cell == 35 }.compact)
  end

  def aabb
    ax = ay = bx = by = nil
    blacks.each do |i|
      y, x = i.divmod(W)
      ax = x if !ax || ax > x
      ay = y if !ay || ay > y
      bx = x if !bx || bx < x
      by = y if !by || by < y
    end
    return ax, ay, bx, by
  end
end

puts MAP.solve
0