結果

問題 No.3323 岩井星式ジャンケン
コンテスト
ユーザー Nanashi.
提出日時 2025-11-01 15:32:58
言語 Ruby
(3.4.1)
結果
AC  
実行時間 487 ms / 2,000 ms
コード長 1,217 bytes
コンパイル時間 807 ms
コンパイル使用メモリ 7,936 KB
実行使用メモリ 28,416 KB
最終ジャッジ日時 2025-11-01 15:33:23
合計ジャッジ時間 8,661 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:37: warning: ambiguous first argument; put parentheses or a space even after `-` operator
Main.rb:60: warning: ambiguous first argument; put parentheses or a space even after `-` operator
Main.rb:18: warning: assigned but unused variable - remaining
Main.rb:3: warning: assigned but unused variable - in_m
Syntax OK

ソースコード

diff #

# frozen_string_literal: true

in_n, in_m = gets.chomp.split.map(&:to_i)
in_s = in_n.times.map { gets.chomp.chars }

def gcp(me, opponent)
  if me == opponent
    0
  elsif [me, opponent] in ["G", "C"] | ["C", "P"] | ["P", "G"]
    1
  else
    -1
  end
end

transposed = in_s.transpose
won = Array.new(in_n, false)
remaining = in_n
result = []
HANDS = %w[G C P]
transposed[..-2].each do |oppos|
  allowed = [0, 0, 0]
  won.zip(oppos) do |done, oppo|
    next if done

    HANDS.each_with_index do |g, i|
      case gcp(g, oppo)
      when 1
        allowed[i] += 1
      when -1
        allowed[i] = -in_n
      end
    end
  end

  if allowed.all?(&:negative?)
    puts -1
    exit
  end

  result << HANDS[allowed.index(&:positive?) || allowed.index(0)]

  won
    .zip(oppos)
    .each_with_index do |(done, oppo), i|
      next if done

      won[i] = true if gcp(result[-1], oppo) == 1
    end
end
[transposed[-1]].each do |oppos|
  allowed = [true, true, true]
  won.zip(oppos) do |done, oppo|
    next if done

    HANDS.each_with_index { |g, i| allowed[i] = false if gcp(g, oppo) != 1 }
  end

  if allowed.all?(false)
    puts -1
    exit
  end

  result << HANDS[allowed.index(true)]
end

puts result.join
0