結果

問題 No.832 麻雀修行中
ユーザー siman
提出日時 2022-11-02 13:38:46
言語 Ruby
(3.4.1)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 845 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-07-17 03:24:07
合計ジャッジ時間 4,365 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 6
other AC * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

S = gets.chomp
L = S.chars.map(&:to_i)

def t(nums)
  nums.uniq.size == 7 && nums.each_slice(2).all? { |a, b| a == b }
end

def f(nums)
  h = nums.tally
  h.default = 0

  queue = []
  1.upto(9) do |n|
    g = h.dup

    if g[n] >= 2
      g[n] -= 2
      queue << g
    end
  end

  until queue.empty?
    g = queue.shift

    if (1..9).all? { |n| g[n] == 0 }
      return true
    end

    1.upto(9) do |n|
      if g[n] >= 3
        k = g.dup
        k[n] -= 3
        queue << k
      end
    end

    1.upto(7) do |n|
      if g[n] >= 1 && g[n + 1] >= 1 && g[n + 2] >= 1
        k = g.dup
        k[n] -= 1
        k[n + 1] -= 1
        k[n + 2] -= 1
        queue << k
      end
    end
  end

  false
end

1.upto(9) do |n|
  next if L.count(n) == 4

  nums = L.dup
  nums << n
  nums.sort!
  
  if t(nums) || f(nums)
    puts n
  end
end
0