結果
問題 | No.355 数当てゲーム(2) |
ユーザー |
|
提出日時 | 2016-04-02 15:18:06 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 121 ms / 2,000 ms |
コード長 | 1,582 bytes |
コンパイル時間 | 167 ms |
コンパイル使用メモリ | 7,552 KB |
実行使用メモリ | 29,128 KB |
平均クエリ数 | 20.67 |
最終ジャッジ日時 | 2024-07-16 23:26:25 |
合計ジャッジ時間 | 9,111 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 52 |
コンパイルメッセージ
Syntax OK
ソースコード
$count = {}def try(n)return $count[n] if $count[n]puts n.join(' ')STDOUT.flushx, y = gets.split.map(&:to_i)$count[n] = x+yexit 0 if 4 == xx+yenddef detect_combinationcount_for_pairs || choose_series_from_pairsenddef count_for_pairspairs = (0..7).each_slice(2).to_apairs.each do |p|n1, n2 = divide_numbers(p)[n1, n2].each do |n|return n if 4 == try(n)end$count[p] = 4 - $count[n1] - $count[n2]end$count[[8, 9]] = 4 - pairs.map{|p| $count[p] }.inject(:+)n = [0, 1, 2, 3]return n if 4 == try(n)nilenddef divide_numbers(exclude_pair)start = exclude_pair[1] + 1a = (start...(start+8)).to_a.map{|i| (9 < i) ? i-10 : i }[ a[0..3], a[4..7] ]enddef choose_series_from_pairspairs = (0..9).each_slice(2).to_ac2_pairs = pairs.select{|p| 2 == $count[p] }c1_pairs = pairs.select{|p| 1 == $count[p] }case c2_pairs.sizewhen 2return c2_pairs.flatten!when 1raise 'must not happen' if 2 != c1_pairs.sizep2 = c2_pairs[0]p0 = c1_pairs[0]p1 = c1_pairs[1]p0.product(p1) do |p|n = p2 + preturn n if 4 == try(n)endraise 'must not happen'when 0raise 'must not happen' if 4 != c1_pairs.size16.times do |i|n = c1_pairs.map.with_index{|p, j| p[(0 == i & (1<<j)) ? 0 : 1] }return n if 4 == try(n)endraise 'must not happen'elseraise 'must not happen'endraise 'must not happen'enddef detect_sequence(n)n.permutation do |n_|try(n_)endenddetect_sequence(detect_combination)