The Blocks Problem

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=37
  2.  의   견   : 명령어 파싱에 대한 연습을 하는 문제라고 생각합니다. 한 가지 언어 특성상 Pointer를 이용한 List 형식으로 코드를 작성할 때 구현에 있어 난이도가 조금 있을 것입니다. 그리하여 STL을 부분적으로 사용했었고
    , 아마 지금 재작성 한다면 table을 비롯하여 모든 부분을 STL으로 작성할 것 같습니다.
  3. 소스
    101.cpp (Language : cpp)
    1. #include <iostream>
    2. #include <deque>
    3. #include <string>
    4. using namespace std;
    5. deque< int > *table;
    6. int *link;
    7. int BlockNum;
    8. void returning( int index );
    9. void moving( int index1, int index2 );
    10. void piling( int index1, int index2 );
    11. void print();
    12. int main( int argc, char **argv )
    13. {
    14.     cin >> BlockNum;
    15.     if( 0 < BlockNum && BlockNum < 25 ) {
    16.         table = new deque< int >[ BlockNum ];
    17.         link = new int[ BlockNum ];
    18.         for( int i = 0; i < BlockNum; i++ ) {
    19.             link[ i ] = i;
    20.             table[ i ].push_back( i );
    21.         }
    22.         string cmd1;
    23.         int a;
    24.         string cmd2;
    25.         int b;
    26.         while( true ) {
    27.             cin >> cmd1;
    28.             if( cmd1 == "quit" ) {
    29.                 print();
    30.                 break;
    31.             }
    32.             cin >> a >> cmd2 >> b;
    33.             if( a != b && link[ a ] != link[ b ] && 0 <= a && a < BlockNum && 0 <= b && b < BlockNum ) {
    34.                 if( cmd1 == "move" ) {
    35.                     if( cmd2 == "onto" ) {
    36.                         // move A onto B
    37.                         returning( a );
    38.                         returning( b );
    39.                         moving( a, b );
    40.                     }
    41.                     else if( cmd2 == "over" ) {
    42.                         // move A over B
    43.                         returning( a );
    44.                         moving( a, b );
    45.                     }
    46.                 }
    47.                 else if( cmd1 == "pile" ) {
    48.                     if( cmd2 == "onto" ) {
    49.                         // pile A onto B
    50.                         returning( b );
    51.                         piling( a, b );
    52.                     }
    53.                     else if( cmd2 == "over" ) {
    54.                         // pile A over B
    55.                         piling( a, b );
    56.                     }
    57.                 }
    58.             }
    59.         }
    60.         delete [] table;
    61.         delete [] link;
    62.     }
    63.     return 0;
    64. }
    65. void returning( int index )
    66. {
    67.     int BlockIndex = link[ index ];
    68.     while( table[ BlockIndex ].back() != index ) {
    69.         int Block = table[ BlockIndex ].back();
    70.         table[ Block ].push_back( Block );
    71.         table[ BlockIndex ].pop_back();
    72.         link[ Block ] = Block;
    73.     }
    74. }
    75. void moving( int index1, int index2 )
    76. {
    77.     int a = link[ index1 ];
    78.     int b = link[ index2 ];
    79.     table[ b ].push_back( table[ a ].back() );
    80.     table[ a ].pop_back();
    81.     link[ index1 ] = link[ index2 ];
    82. }
    83. void piling( int index1, int index2 )
    84. {
    85.     deque< int > temp;
    86.     int BlockIndex1 = link[ index1 ];
    87.     while( table[ BlockIndex1 ].back() != index1 ) {
    88.         temp.push_back( table[ BlockIndex1 ].back() );
    89.         table[ BlockIndex1 ].pop_back();
    90.     }
    91.     temp.push_back( table[ BlockIndex1 ].back() );
    92.     table[ BlockIndex1 ].pop_back();
    93.     int BlockIndex2 = link[ index2 ];
    94.     while( !temp.empty() ) {
    95.         int linkTemp = temp.back();
    96.         temp.pop_back();
    97.         table[ BlockIndex2 ].push_back( linkTemp );
    98.         link[ linkTemp ] = link[ index2 ];
    99.     }
    100. }
    101. void print()
    102. {
    103.     deque< int >::iterator prt;
    104.     for( int i = 0; i < BlockNum; i++ ) {
    105.         cout << i << ":";
    106.         prt = table[ i ].begin();
    107.         while( prt != table[ i ].end() ) {
    108.             cout << " " << *prt;
    109.             prt++;
    110.         }
    111.         cout << endl;
    112.     }
    113. }
크리에이티브 커먼즈 라이센스
Creative Commons License

"Algorithm / Acm" 분류의 다른 글

LCD Display (3)2008/02/17
Maximum Sum (0)2008/02/17
The Skyline Problem (2)2008/02/17
Ecological Bin Packing (0)2008/02/17
3n+1 Problem (0)2008/02/17
2008/02/17 08:30 2008/02/17 08:30
TAG , , , ,

트랙백 주소 :: http://www.filewiki.net/tc/trackback/5

댓글을 달아 주세요