Sicily 1256. Bingo!

做這道題的時候,數據是12S1A,看完題目後覺得是因爲這道題太長又晦澀,而又缺乏足夠詳細的說明,所以問津者寥寥,所以就放手一試,沒想到還真是能AC。

題目意思是關於一個Bingo的遊戲,首先給出7個數字,前5個數字分別表示每個對應列已經出現了的數字個數,第6個數字(X)表示pattern的數目,第7個數字(Y)表示winning pattern需要由X個pattern中的Y個組合而成,然後給出X個pattern。我們的任務就是求出至少還需要出現多少個數字,才能夠Bingo(即組合出winning pattern)。

我的方法是,在X個pattern中選出Y個,逐一組合出所有的CYX個winning pattern,然後分析至少(這種情況下下,我們希望已經出現了的數字都出現在需要的地方,才能夠達到至少這一界限)還需要多少個數字才能夠Bingo,將這些結果中最少的那個輸出即可。

Run Time: 0.05sec

Run Memory: 304KB

Code Length: 1431Bytes

Submit Time: 2012-02-16 21:25:47

// Problem#: 1256
// Submission#: 1211213
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <cstdio>
#include <string>
using namespace std;

int bingo[ 5 ], X, Y, fewest;
char patterns[ 19 ][ 26 ];

string combine( const string& s1, char s2[] );
void analyse( const string& s );
void choose( int m, int n, string s );

int main()
{
    int n;
    int i, j;

    scanf( "%d", &n );
    while ( n-- ) {
        scanf( "%d%d%d%d%d%d%d", &bingo[ 0 ], &bingo[ 1 ], &bingo[ 2 ], &bingo[ 3 ], &bingo[ 4 ], &X, &Y );
        for ( i = 0; i < 5; i++ ) {
            for ( j = 0; j < X; j++ )
                scanf( "%s", &patterns[ j ][ 5 * i ] );
        }

        fewest = 25;
        choose( 0, 0, string( 25, 'O' ) );
        printf( "%d\n", fewest );
    }

    return 0;

}


string combine( const string& s1, char s2[] ) {
    string r = string( 25, 'O' );
    for ( int i = 0; i < 25; i++ )
        r[ i ] = ( s1[ i ] == 'X' || s2[ i ] == 'X' ? 'X': 'O' );
    r[ 12 ] = 'O';
    return r;
}

void analyse( const string& s ) {
    int i, need[ 5 ] = { 0 }, sum = 0;
    for ( i = 0; i < 25; i++ ) {
        if ( s[ i ] == 'X' )
            need[ i % 5 ]++;
    }
    for ( i = 0; i < 5; i++ )
        sum += max( 0, need[ i ] - bingo[ i ] );
    if ( sum < fewest )
        fewest = sum;
}

void choose( int m, int n, string s ) {
    if ( n == Y )
        analyse( s );
    else if ( Y - n <= X - m ) {
        for ( int i = m; i < X; i++ )
            choose( i + 1, n + 1, combine( s, patterns[ i ] ) );
    }
}                                 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章