結果

問題 No.43 野球の試合
ユーザー simansiman
提出日時 2016-03-25 02:10:54
言語 Ruby
(3.3.0)
結果
AC  
実行時間 178 ms / 5,000 ms
コード長 1,288 bytes
コンパイル時間 74 ms
コンパイル使用メモリ 11,532 KB
実行使用メモリ 16,076 KB
最終ジャッジ日時 2023-08-27 01:35:19
合計ジャッジ時間 1,854 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
15,128 KB
testcase_01 AC 73 ms
15,276 KB
testcase_02 AC 73 ms
15,236 KB
testcase_03 AC 73 ms
15,060 KB
testcase_04 AC 71 ms
15,128 KB
testcase_05 AC 73 ms
15,232 KB
testcase_06 AC 82 ms
15,072 KB
testcase_07 AC 178 ms
16,076 KB
testcase_08 AC 75 ms
15,236 KB
testcase_09 AC 75 ms
15,276 KB
testcase_10 AC 75 ms
14,992 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Yukicoder
  attr_accessor :matrix

  NONE = 0
  WIN = 1
  LOSE = 2

  def initialize
    @n = gets.to_i
    @matrix = Array.new(@n){ Array.new(@n, -1) }
    @answer = Float::INFINITY

    @n.times do |i|
      s = gets.chomp

      s.chars.each_with_index do |ch, j|
        case ch
        when '-'
          matrix[i][j] = NONE
        when 'o'
          matrix[i][j] = WIN
        when 'x'
          matrix[i][j] = LOSE
        end
      end
    end

    dfs(0, 0)

    puts @answer
  end

  def dfs(i, j)
    if @n-1 == i && @n-1 == j
      check
    else
      if matrix[i][j] == NONE
        matrix[i][j] = WIN
        matrix[j][i] = LOSE
        if j == @n-1
          dfs(i+1, 0)
        else
          dfs(i, j+1)
        end

        matrix[i][j] = LOSE
        matrix[j][i] = WIN
        if j == @n-1
          dfs(i+1, 0)
        else
          dfs(i, j+1)
        end

        matrix[i][j] = NONE
        matrix[j][i] = NONE
      else
        if j == @n-1
          dfs(i+1, 0)
        else
          dfs(i, j+1)
        end
      end
    end
  end

  def check
    win_count = matrix[0].count(WIN)

    rank = 1 + matrix[1..-1].map{|list|
      list.count(WIN) || 0
    }.select{|n| n > win_count}.uniq.size

    @answer = [@answer, rank].min
  end
end

Yukicoder.new
0