結果

問題 No.13 囲みたい!
ユーザー letrangerjpletrangerjp
提出日時 2017-05-20 15:51:32
言語 Ruby
(3.3.0)
結果
AC  
実行時間 123 ms / 5,000 ms
コード長 936 bytes
コンパイル時間 45 ms
コンパイル使用メモリ 11,960 KB
実行使用メモリ 16,940 KB
最終ジャッジ日時 2023-10-19 04:12:04
合計ジャッジ時間 2,535 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
16,048 KB
testcase_01 AC 91 ms
16,048 KB
testcase_02 AC 96 ms
16,048 KB
testcase_03 AC 98 ms
16,940 KB
testcase_04 AC 95 ms
16,372 KB
testcase_05 AC 119 ms
16,912 KB
testcase_06 AC 97 ms
16,376 KB
testcase_07 AC 117 ms
16,632 KB
testcase_08 AC 123 ms
16,464 KB
testcase_09 AC 120 ms
16,464 KB
testcase_10 AC 98 ms
16,048 KB
testcase_11 AC 113 ms
16,356 KB
testcase_12 AC 93 ms
16,048 KB
testcase_13 AC 101 ms
16,240 KB
testcase_14 AC 102 ms
16,236 KB
testcase_15 AC 95 ms
16,048 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def f(board, width, height)
  visited = height.times.map{[0]*width}
  g = lambda{|h, w, n, cnt=0, from=nil|
    return if !h.between?(0, height-1)
    return if !w.between?(0, width-1)
    return if n != board[h][w]
    if visited[h][w] > 0
      # p [n, cnt, [h,w]]
      return cnt + visited[h][w] > 4
    end
    cnt += 1
    visited[h][w] = cnt
    from != :l && g.(h, w-1, n, cnt, :r) ||
    from != :r && g.(h, w+1, n, cnt, :l) ||
    from != :u && g.(h-1, w, n, cnt, :d) ||
    from != :d && g.(h+1, w, n, cnt, :u)
  }
  # visited.each{|l| puts l.join}

  height.times{|h|
    width.times{|w|
      next if visited[h][w] > 0
      if g.(h, w, board[h][w])
        # p [h,w]
        # visited.each{|l| puts l.join}
        return true
      end
    }
  }
  # visited.each{|l| puts l.join}
  false
end

W, H = gets.split.take(2).map(&:to_i)
M = H.times.map{gets.split.take(W).map(&:to_i)}
puts f(M, W, H) ? :possible : :impossible
0