SRM453 Div2 Medium(500) TheBasketballDivTwo

TheBasketballDivTwo

#include <vector>
#include <string>
#include <algorithm>

using namespace std;

class TheBasketballDivTwo
{
    int n;
    vector<string> t;
    int play( int p );
public:
    int find( vector<string> table );
};

int TheBasketballDivTwo::find( vector<string> table )
{
    n = (int)table.size();
    t = table;

    return play( 0 );
}

int TheBasketballDivTwo::play( int p )
{
    if ( p >= n*n )
    {
        vector<int> win( n, 0 );

        for ( int i=0; i<n; i++ )
        for ( int j=0; j<n; j++ )
        {
            if ( t[i][j] == 'W' )  win[i]++;
            if ( t[i][j] == 'L' )  win[j]++;
        }

        return *max_element( win.begin(), win.end() );
    }
    else
    {
        if ( t[p/n][p%n] != '?' )
        {
            return play( p+1 );
        }
        else
        {
            int r = 99;

            t[p/n][p%n] = 'W';
            r = min( r, play( p+1 ) );
            t[p/n][p%n] = 'L';
            r = min( r, play( p+1 ) );
            t[p/n][p%n] = '?';
            
            return r;
        }   
    }
}