def sol n,d,w,c,memo if memo[c] <= n return memo[c] end if c == 0 return w.uniq.sort.reverse.index(w[0]) + 1 end ans = n n.times{|i| (i + 1).upto(n - 1){|j| if d[i][j] == '-' d[i][j] = 'o' d[j][i] = 'x' w[i] += 1 ans = [ans, sol(n,d,w,c - 2,memo)].min d[i][j] = 'x' d[j][i] = 'o' w[i] -= 1 w[j] += 1 ans = [ans, sol(n,d,w,c - 2,memo)].min d[i][j] = '-' d[j][i] = '-' w[j] -= 1 end } } memo[c] = [ans, memo[c]].min return ans end n = gets.to_i d = Array.new(n) w = Array.new(n,0) c = 0 n.times{|i| d[i] = gets.chomp.split('') n.times{|j| if d[i][j] == 'o' w[i] += 1 elsif d[i][j] == '-' c += 1 end } } memo = Array.new(c + 1, n + 1) p sol(n,d,w,c,memo)