class Calc0043 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @n = args.shift.first.to_i @wl = args.map(&:first).map { |l| l.split(//) } end def run no_games = (0...@n).flat_map { |i| ((i + 1)...@n).map { |j| @wl[i][j] == '-' ? [i, j] : nil } }.compact if no_games.empty? calc_rank(@wl) else cands = no_games.map { |n| ['x', 'o'] } cands.first.product(*cands.drop(1)).map { |cand| calc(no_games, cand) }.min end end def calc(no_games, cand) wl = @wl.map { |r| r.clone } no_games.zip(cand) { |(i, j), c| wl[i][j] = c wl[j][i] = c == 'o' ? 'x' : 'o' } calc_rank(wl) end def calc_rank(wl) ws = wl.map { |r| r.count { |c| c == 'o' } } w0 = ws[0] ws.uniq.sort.reverse.index(w0) + 1 end end puts Calc0043.new(STDIN.readlines).run if __FILE__ == $0