SRM483 Div2 Medium(500) MovieSeating

MovieSeating

numFriends=1の場合を別に扱う必要がある。

#include <string>
#include <vector>

using namespace std;

long long P( int n, int m )
{
    long long r = 1;
    for ( int i=0; i<m; i++ )
        r *= n-i;
    return r;
}

class MovieSeating{public:
long long getSeatings( int numFriends, vector <string> hall )
{
    int h = (int)hall.size();
    int w = (int)hall[0].size();

    long long ans = 0;

    if ( numFriends > 1 )
    {
        for ( int y=0; y<h; y++ )
        {
            int c = 0;
            for ( int x=0; x<w; x++ )
                if ( hall[y][x] == '.' )
                    c++;
            if ( c >= numFriends )
                ans += P( c, numFriends );
        }

        for ( int x=0; x<w; x++ )
        {
            int c = 0;
            for ( int y=0; y<h; y++ )
                if ( hall[y][x] == '.' )
                    c++;
            if ( c >= numFriends )
                ans += P( c, numFriends );
        }
    }
    else
    {
        for ( int y=0; y<h; y++ )
        for ( int x=0; x<w; x++ )
            if ( hall[y][x] == '.' )
                ans++;
    }

    return ans;
}};