結果

問題 No.13 囲みたい!
ユーザー wotsushiwotsushi
提出日時 2017-05-24 21:11:58
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 554 bytes
コンパイル時間 317 ms
コンパイル使用メモリ 11,944 KB
実行使用メモリ 23,228 KB
最終ジャッジ日時 2023-10-19 22:33:10
合計ジャッジ時間 2,749 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
16,132 KB
testcase_01 AC 88 ms
16,128 KB
testcase_02 AC 88 ms
16,132 KB
testcase_03 RE -
testcase_04 AC 95 ms
17,120 KB
testcase_05 RE -
testcase_06 AC 96 ms
17,480 KB
testcase_07 AC 139 ms
17,572 KB
testcase_08 AC 142 ms
17,164 KB
testcase_09 AC 138 ms
17,176 KB
testcase_10 AC 95 ms
16,320 KB
testcase_11 AC 127 ms
16,784 KB
testcase_12 AC 89 ms
16,132 KB
testcase_13 AC 103 ms
16,468 KB
testcase_14 AC 105 ms
16,468 KB
testcase_15 AC 90 ms
16,132 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, dp)
  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, dp)}
  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, dp)}
        "possible"
      else
        "impossible"
      end
puts ans
0