結果

問題 No.179 塗り分け
ユーザー d2verb
提出日時 2017-12-27 04:48:33
言語 Crystal
(1.14.0)
結果
AC  
実行時間 243 ms / 3,000 ms
コード長 797 bytes
コンパイル時間 11,789 ms
コンパイル使用メモリ 296,076 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-30 19:13:38
合計ジャッジ時間 15,839 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 40
権限があれば一括ダウンロードができます
コンパイルメッセージ
In Main.cr:8:22

 8 | (-(h-1)...h).to_a.product((-(w-1)...w).to_a) do |dy, dx|
                        ^------
Warning: Deprecated Array(Int32)#product. Use `Indexable#each_cartesian(*others : Indexable, &block)` instead

In Main.cr:15:18

 15 | (0...h).to_a.product((0...w).to_a) do |y, x|
                   ^------
Warning: Deprecated Array(Int32)#product. Use `Indexable#each_cartesian(*others : Indexable, &block)` instead

A total of 2 warnings were found.

ソースコード

diff #

h, w = gets.not_nil!.split(" ").map{|x| x.to_i}
rect = [] of Array(String)
1.upto(h) do |i|
  rect << gets.not_nil!.split("")
end

def ok?(rect, h, w)
  (-(h-1)...h).to_a.product((-(w-1)...w).to_a) do |dy, dx|
    next if dx == 0 && dy == 0

    _rect = rect.clone
    ok = true
    touched = false

    (0...h).to_a.product((0...w).to_a) do |y, x|
      next if _rect[y][x] == "."

      ny = y + dy
      nx = x + dx

      if nx < 0 || nx >= w || ny < 0 || ny >= h
        ok = false
        break
      end

      if _rect[y][x] == _rect[ny][nx]
        touched = true
        _rect[y][x] = "."
        _rect[ny][nx] = "."
      else
        ok = false
        break
      end
    end

    ok = ok && touched
    return true if ok
  end
  return false
end

puts ok?(rect, h, w) ? "YES" : "NO"
0