'priority_queue'에 해당되는 글 1건

  1. 2008/02/17 귀차니스트 Ecological Bin Packing

Ecological Bin Packing

Algorithm/Acm 2008/02/17 08:30 귀차니스트
  1. 문제링크 : http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=38
  2.  의   견   : 나올 수 있는 경우의 수는 3! 개 입니다. 즉, 경우의 수가 적으므로 6가지 경우에 대한 측정값들을 수동으로 계산하여 출력할 수 있습니다. 하지만, 일반적으로 계산을 해보고자 하는 마음으로 priority_queue와 string을 짝으로 조합하여 구현을 했습니다.
  3. 소스
    103.cpp (Language : cpp)
    1. #include <iostream>
    2. #include <queue>
    3. #include <string>
    4. struct SortNode {
    5.     int Total;
    6.     std::string OutputStr;
    7. };
    8. bool operator < ( const SortNode &Left, const SortNode &Right )
    9. {
    10.     size_t LeftLength = Left.OutputStr.length();
    11.     size_t RightLength = Right.OutputStr.length();
    12.     if( ( Left.Total == Right.Total ) && ( LeftLength == RightLength ) )    {
    13.         for( int i = 0; i < 3; i++ )    {
    14.             char LeftChar = Left.OutputStr[ i ];
    15.             char RightChar = Right.OutputStr[ i ];
    16.             if( LeftChar == RightChar )
    17.                 continue;
    18.             else if( LeftChar > RightChar )
    19.                 return true;
    20.             else
    21.                 return false;
    22.         }
    23.     }
    24.     return ( Left.Total > Right.Total );
    25. }
    26. int main( int argc, char **argv )
    27. {
    28.     unsigned int Bottle[ 9 ] = { 0, };
    29.     while( std::cin >> Bottle[ 0 ] >> Bottle[ 1 ] >> Bottle[ 2 ] >> Bottle[ 3 ] >> Bottle[ 4 ] >> Bottle[ 5 ] >> Bottle[ 6 ] >> Bottle[ 7 ] >> Bottle[ 8 ] )    {
    30.         std::priority_queue< SortNode > BackTracking;
    31.         SortNode Temp;
    32.         Temp.Total = 0;
    33.         BackTracking.push( Temp );
    34.         while( true )   {
    35.             SortNode ProcessNode = BackTracking.top();
    36.             BackTracking.pop();
    37.             size_t ProcessStrLength = ProcessNode.OutputStr.length();
    38.             if( ProcessStrLength == 3 ) {
    39.                 std::cout << ProcessNode.OutputStr << " " << ProcessNode.Total << std::endl;
    40.                 break;
    41.             }
    42.             ProcessStrLength *= 3;
    43.             if( ProcessNode.OutputStr.find( "C" ) == UINT_MAX ) {
    44.                 SortNode CNode( ProcessNode );
    45.                 CNode.Total += ( Bottle[ ProcessStrLength ] + Bottle[ ProcessStrLength + 1 ] );
    46.                 CNode.OutputStr += "C";
    47.                 BackTracking.push( CNode );
    48.             }
    49.             if( ProcessNode.OutputStr.find( "G" ) == UINT_MAX ) {
    50.                 SortNode GNode( ProcessNode );
    51.                 GNode.Total += ( Bottle[ ProcessStrLength ] + Bottle[ ProcessStrLength + 2 ] );
    52.                 GNode.OutputStr += "G";
    53.                 BackTracking.push( GNode );
    54.             }
    55.             if( ProcessNode.OutputStr.find( "B" ) == UINT_MAX ) {
    56.                 SortNode BNode( ProcessNode );
    57.                 BNode.Total += ( Bottle[ ProcessStrLength + 1 ] + Bottle[ ProcessStrLength + 2 ] );
    58.                 BNode.OutputStr += "B";
    59.                 BackTracking.push( BNode );
    60.             }
    61.         }
    62.     }
    63.     return 0;
    64. }



크리에이티브 커먼즈 라이센스
Creative Commons License

"Algorithm / Acm" 분류의 다른 글

The Blocks Problem (0)2008/02/17
3n+1 Problem (0)2008/02/17
The Skyline Problem (2)2008/02/17
Maximum Sum (0)2008/02/17
LCD Display (3)2008/02/17
2008/02/17 08:30 2008/02/17 08:30

댓글을 달아 주세요