結果

問題 No.355 数当てゲーム(2)
ユーザー shi-moshi-mo
提出日時 2016-04-02 15:18:06
言語 Ruby
(3.3.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 1,582 bytes
コンパイル時間 50 ms
コンパイル使用メモリ 11,464 KB
実行使用メモリ 31,448 KB
平均クエリ数 20.67
最終ジャッジ日時 2023-09-23 23:35:50
合計ジャッジ時間 7,263 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 101 ms
30,832 KB
testcase_01 AC 94 ms
30,696 KB
testcase_02 AC 90 ms
31,388 KB
testcase_03 AC 90 ms
30,872 KB
testcase_04 AC 93 ms
31,416 KB
testcase_05 AC 88 ms
30,836 KB
testcase_06 AC 90 ms
30,888 KB
testcase_07 AC 88 ms
31,100 KB
testcase_08 AC 91 ms
30,728 KB
testcase_09 AC 90 ms
31,168 KB
testcase_10 AC 92 ms
31,236 KB
testcase_11 AC 90 ms
31,280 KB
testcase_12 AC 93 ms
31,136 KB
testcase_13 AC 90 ms
31,016 KB
testcase_14 AC 88 ms
31,412 KB
testcase_15 AC 92 ms
31,272 KB
testcase_16 AC 91 ms
31,104 KB
testcase_17 AC 88 ms
31,448 KB
testcase_18 AC 89 ms
30,776 KB
testcase_19 AC 91 ms
31,284 KB
testcase_20 AC 86 ms
30,780 KB
testcase_21 AC 88 ms
31,372 KB
testcase_22 AC 87 ms
31,364 KB
testcase_23 AC 92 ms
30,812 KB
testcase_24 AC 90 ms
31,304 KB
testcase_25 AC 88 ms
30,904 KB
testcase_26 AC 87 ms
31,092 KB
testcase_27 AC 91 ms
31,356 KB
testcase_28 AC 92 ms
30,952 KB
testcase_29 AC 92 ms
31,072 KB
testcase_30 AC 87 ms
31,072 KB
testcase_31 AC 90 ms
31,148 KB
testcase_32 AC 96 ms
31,132 KB
testcase_33 AC 88 ms
31,308 KB
testcase_34 AC 88 ms
31,076 KB
testcase_35 AC 90 ms
30,856 KB
testcase_36 AC 88 ms
31,096 KB
testcase_37 AC 89 ms
31,352 KB
testcase_38 AC 92 ms
30,760 KB
testcase_39 AC 89 ms
31,104 KB
testcase_40 AC 89 ms
30,936 KB
testcase_41 AC 89 ms
30,864 KB
testcase_42 AC 86 ms
31,068 KB
testcase_43 AC 88 ms
31,008 KB
testcase_44 AC 89 ms
30,872 KB
testcase_45 AC 90 ms
31,244 KB
testcase_46 AC 89 ms
31,384 KB
testcase_47 AC 92 ms
30,880 KB
testcase_48 AC 93 ms
30,920 KB
testcase_49 AC 91 ms
31,372 KB
testcase_50 AC 89 ms
30,792 KB
testcase_51 AC 91 ms
30,872 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

$count = {}

def try(n)
  return $count[n] if $count[n]

  puts n.join(' ')
  STDOUT.flush

  x, y = gets.split.map(&:to_i)
  $count[n] = x+y

  exit 0 if 4 == x
  x+y
end

def detect_combination
  count_for_pairs || choose_series_from_pairs
end

def count_for_pairs 
  pairs = (0..7).each_slice(2).to_a
  pairs.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)
  nil
end

def divide_numbers(exclude_pair)
  start = exclude_pair[1] + 1
  a = (start...(start+8)).to_a.map{|i| (9 < i) ? i-10 : i }
  [ a[0..3], a[4..7] ]
end

def choose_series_from_pairs
  pairs = (0..9).each_slice(2).to_a
  c2_pairs = pairs.select{|p| 2 == $count[p] }
  c1_pairs = pairs.select{|p| 1 == $count[p] }

  case c2_pairs.size
  when 2
    return c2_pairs.flatten!
  when 1
    raise 'must not happen' if 2 != c1_pairs.size
    p2 = c2_pairs[0]
    p0 = c1_pairs[0]
    p1 = c1_pairs[1]
    p0.product(p1) do |p|
      n = p2 + p
      return n if 4 == try(n)
    end
    raise 'must not happen'
  when 0
    raise 'must not happen' if 4 != c1_pairs.size
    16.times do |i|
      n = c1_pairs.map.with_index{|p, j| p[(0 == i & (1<<j)) ? 0 : 1] }
      return n if 4 == try(n)
    end
    raise 'must not happen'
  else
    raise 'must not happen'
  end

  raise 'must not happen'
end

def detect_sequence(n)
  n.permutation do |n_|
    try(n_)
  end
end

detect_sequence(detect_combination)
0