結果

問題 No.13 囲みたい!
ユーザー wotsushiwotsushi
提出日時 2017-05-24 21:48:51
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 542 bytes
コンパイル時間 126 ms
コンパイル使用メモリ 11,932 KB
実行使用メモリ 23,360 KB
最終ジャッジ日時 2023-10-19 22:37:20
合計ジャッジ時間 3,146 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
16,084 KB
testcase_01 AC 86 ms
16,084 KB
testcase_02 AC 91 ms
16,084 KB
testcase_03 RE -
testcase_04 AC 94 ms
17,068 KB
testcase_05 RE -
testcase_06 AC 93 ms
17,428 KB
testcase_07 AC 135 ms
17,520 KB
testcase_08 AC 138 ms
17,116 KB
testcase_09 AC 139 ms
17,116 KB
testcase_10 AC 94 ms
16,272 KB
testcase_11 AC 128 ms
16,736 KB
testcase_12 AC 91 ms
16,084 KB
testcase_13 AC 103 ms
16,420 KB
testcase_14 AC 105 ms
16,420 KB
testcase_15 AC 89 ms
16,084 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

W, H = gets.split.map(&:to_i)
M = (1..H).map {gets.split.map(&:to_i)}
DP = Array.new(H) {Array.new(W, false)}
def f(i, j, p, q)
  if not i.between?(0, H - 1) or not j.between?(0, W - 1) or M[i][j] != M[p][q]
    false
  elsif DP[i][j]
    true
  else
    DP[i][j] = true
    [[i, j + 1], [i + 1, j], [i, j - 1], [i - 1, j]].any? {|a, b| [a, b] != [p, q] and f(a, b, i, j)}
  end
end
ans = if (0...H).to_a.product((0...W).to_a).any? {|i, j| not DP[i][j] and f(i, j, i, j)}
        "possible"
      else
        "impossible"
      end
puts ans
0