結果

問題 No.179 塗り分け
ユーザー shi-moshi-mo
提出日時 2016-03-31 13:54:55
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,644 ms / 3,000 ms
コード長 1,026 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 11,404 KB
実行使用メモリ 21,712 KB
最終ジャッジ日時 2023-09-30 20:48:04
合計ジャッジ時間 9,655 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,168 KB
testcase_01 AC 83 ms
15,124 KB
testcase_02 AC 84 ms
15,308 KB
testcase_03 AC 83 ms
15,172 KB
testcase_04 AC 84 ms
15,164 KB
testcase_05 AC 109 ms
17,192 KB
testcase_06 AC 84 ms
15,144 KB
testcase_07 AC 82 ms
15,020 KB
testcase_08 AC 169 ms
21,424 KB
testcase_09 AC 241 ms
18,772 KB
testcase_10 AC 88 ms
16,944 KB
testcase_11 AC 88 ms
16,600 KB
testcase_12 AC 242 ms
21,196 KB
testcase_13 AC 84 ms
15,308 KB
testcase_14 AC 87 ms
15,300 KB
testcase_15 AC 84 ms
15,032 KB
testcase_16 AC 83 ms
15,152 KB
testcase_17 AC 84 ms
15,280 KB
testcase_18 AC 91 ms
17,976 KB
testcase_19 AC 89 ms
17,900 KB
testcase_20 AC 199 ms
21,512 KB
testcase_21 AC 573 ms
15,936 KB
testcase_22 AC 1,644 ms
15,568 KB
testcase_23 AC 87 ms
16,588 KB
testcase_24 AC 258 ms
18,448 KB
testcase_25 AC 91 ms
17,216 KB
testcase_26 AC 110 ms
21,460 KB
testcase_27 AC 190 ms
21,540 KB
testcase_28 AC 97 ms
21,616 KB
testcase_29 AC 183 ms
21,420 KB
testcase_30 AC 138 ms
21,580 KB
testcase_31 AC 200 ms
21,712 KB
testcase_32 AC 116 ms
21,516 KB
testcase_33 AC 187 ms
21,400 KB
testcase_34 AC 110 ms
21,512 KB
testcase_35 AC 190 ms
21,544 KB
testcase_36 AC 84 ms
15,308 KB
testcase_37 AC 84 ms
15,236 KB
testcase_38 AC 84 ms
15,240 KB
testcase_39 AC 85 ms
15,036 KB
testcase_40 AC 84 ms
15,172 KB
testcase_41 AC 85 ms
15,256 KB
testcase_42 AC 86 ms
15,248 KB
testcase_43 AC 93 ms
16,576 KB
testcase_44 AC 104 ms
18,336 KB
testcase_45 AC 86 ms
15,548 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

h, w = gets.split.map(&:to_i)
s = h.times.map{ gets }
board = { h: h, w: w, s: s }

def is_empty?(board)
  board[:s].each do |row|
    return false if row =~ /#/o
  end
  true
end

def include_index?(board, i, j)
  0 <= i && i < board[:h] && 0 <= j && j < board[:w]
end

def can_paint_bicolor?(board, dx, dy)
  h, w = board[:h], board[:w]
  checked = Array.new(h){ Array.new(w, false) }

  result = true
  h.times do |i|
    break unless result

    w.times do |j|
      break unless result

      next if checked[i][j]
      next if '#' != board[:s][i][j]

      checked[i][j] = true
      ni, nj = i + dx, j + dy
      if !include_index?(board, ni, nj) || '#' != board[:s][ni][nj]
        result = false
        break
      end
      checked[ni][nj] = true
    end
  end
  result
end

if is_empty?(board)
  puts 'NO'
  exit 0
end

ok = false
h.times do |dx|
  break if ok
  ((1-w)...w).each do |dy|
    break if ok
    next if 0 == dx && 0 == dy

    ok |= can_paint_bicolor?(board, dx, dy)
  end
end
puts ok ? 'YES' : 'NO'
0