블로그는 귀차니즘

First Sensation
  • 공지
  • 지역로그
  • 태그
  • 방명록

The Blocks Problem

Algorithm 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.  
    5. using namespace std;
    6.  
    7. deque< int > *table;
    8. int *link;
    9. int BlockNum;
    10.  
    11. void returning( int index );
    12. void moving( int index1, int index2 );
    13. void piling( int index1, int index2 );
    14. void print();
    15.  
    16. int main( int argc, char **argv )
    17. {
    18.     cin >> BlockNum;
    19.     if( 0 < BlockNum && BlockNum < 25 ) {
    20.         table = new deque< int >[ BlockNum ];
    21.         link = new int[ BlockNum ];
    22.         for( int i = 0; i < BlockNum; i++ ) {
    23.             link[ i ] = i;
    24.             table[ i ].push_back( i );
    25.         }
    26.         string cmd1;
    27.         int a;
    28.         string cmd2;
    29.         int b;
    30.         while( true ) {
    31.             cin >> cmd1;
    32.             if( cmd1 == "quit" ) {
    33.                 print();
    34.                 break;
    35.             }
    36.             cin >> a >> cmd2 >> b;
    37.             if( a != b && link[ a ] != link[ b ] && 0 <= a && a < BlockNum && 0 <= b && b < BlockNum ) {
    38.                 if( cmd1 == "move" ) {
    39.                     if( cmd2 == "onto" ) {
    40.                         // move A onto B
    41.  
    42.                         returning( a );
    43.                         returning( b );
    44.                         moving( a, b );
    45.                     }
    46.                     else if( cmd2 == "over" ) {
    47.                         // move A over B
    48.                         returning( a );
    49.                         moving( a, b );
    50.                     }
    51.                 }
    52.                 else if( cmd1 == "pile" ) {
    53.                     if( cmd2 == "onto" ) {
    54.                         // pile A onto B
    55.  
    56.                         returning( b );
    57.                         piling( a, b );
    58.  
    59.                     }
    60.                     else if( cmd2 == "over" ) {
    61.                         // pile A over B
    62.                         piling( a, b );
    63.                     }
    64.                 }
    65.             }
    66.         }
    67.         delete [] table;
    68.         delete [] link;
    69.     }
    70.     return 0;
    71. }
    72.  
    73. void returning( int index )
    74. {
    75.     int BlockIndex = link[ index ];
    76.     while( table[ BlockIndex ].back() != index ) {
    77.  
    78.         int Block = table[ BlockIndex ].back();
    79.         table[ Block ].push_back( Block );
    80.         table[ BlockIndex ].pop_back();
    81.         link[ Block ] = Block;
    82.     }
    83. }
    84.  
    85. void moving( int index1, int index2 )
    86. {
    87.     int a = link[ index1 ];
    88.     int b = link[ index2 ];
    89.     table[ b ].push_back( table[ a ].back() );
    90.     table[ a ].pop_back();
    91.     link[ index1 ] = link[ index2 ];
    92. }
    93.  
    94. void piling( int index1, int index2 )
    95. {
    96.     deque< int > temp;
    97.     int BlockIndex1 = link[ index1 ];
    98.     while( table[ BlockIndex1 ].back() != index1 ) {
    99.         temp.push_back( table[ BlockIndex1 ].back() );
    100.         table[ BlockIndex1 ].pop_back();
    101.     }
    102.     temp.push_back( table[ BlockIndex1 ].back() );
    103.     table[ BlockIndex1 ].pop_back();
    104.     int BlockIndex2 = link[ index2 ];
    105.     while( !temp.empty() ) {
    106.         int linkTemp = temp.back();
    107.         temp.pop_back();
    108.         table[ BlockIndex2 ].push_back( linkTemp );
    109.         link[ linkTemp ] = link[ index2 ];
    110.     }
    111. }
    112.  
    113. void print()
    114. {
    115.     deque< int >::iterator prt;
    116.     for( int i = 0; i < BlockNum; i++ ) {
    117.         cout << i << ":";
    118.         prt = table[ i ].begin();
    119.         while( prt != table[ i ].end() ) {
    120.             cout << " " << *prt;
    121.             prt++;
    122.         }
    123.         cout << endl;
    124.     }
    125. }
크리에이티브 커먼즈 라이센스
Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-동일조건변경허락 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

"Algorithm" 분류의 다른 글

Algorithm Traning Book - 다섯번째 문제 (0)2008/06/15
Algorithm Traning Book - 세 번째 문제 (0)2008/06/10
Algorithm Traning Book - 두 번째 문제 (0)2008/06/09
3n+1 Problem (0)2008/02/17
Maximum Sum (0)2008/02/17
2008/02/17 08:30 2008/02/17 08:30
TAG ACM, ACM-ICPC, C++, ICPC, STL
받은 트랙백이 없고, 댓글이 없습니다.

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

댓글을 달아 주세요

◀ 이전페이지 1 ... 108 109 110 111 112 113 114 115 116 ... 118 다음페이지 ▶

블로그 이미지
First Sensation 귀차니스트
rss
  • 관리자
  • 글쓰기

카테고리

  • 전체 (118)
    • Computer (3)
    • Language (14)
    • Reverse Engineering (1)
    • Algorithm (9)
    • TopCoder (3)
    • Library (2)
    • Programming (21)
    • Programming Tip (9)
    • PSP-Programming (10)
    • Program (5)
    • Small Talk (33)
    • Document (4)
    • OS Develope (4)

최근에 올라온 글

  • Script Interpreter - b....
  • VirtualHttpServer - 가.... (2)
  • 음.. 여러가지 일이 있.... (2)
  • 어후.. 드디어 인터럽트....
  • Kernel Image에 어이없....

최근에 달린 댓글

  • 헠 ㅋ 다음에도 들러주세용 ㅋㅋ. 귀차니스트 03/09
  • ㅎㅎ RSS로 첨 온 글이네.ㅋ. 당구리 02/22
  • 음.. 한글화 파일 0.5 버젼은.... 귀차니스트 02/22
  • 관리자만 볼 수 있는 댓글입.... 비밀방문자 01/30
  • 어떤 의미이신지 잘 모르겠네.... 귀차니스트 01/23

달력

«   2010/03   »
일 월 화 수 목 금 토
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

링크

  • kkamagui 프로그래밍 세상.
  • runner님의 이글루.
  • 당구리의 마굿간.
  • 동우fly.
  • 류광의 번역 이야기.
  • 서광열의 프로그래밍 언....
  • 준호씨의 블로그.
  • 최익필의 이름없는 블로그.
  • 위키는 귀차니즘.

최근에 받은 트랙백

  • 한게임 테트리스 인공지.... 고니's Life 2009
  • ACM 706 (Uva ID) : LCD.... 알고리즘 트레이닝 : Oh... 2009
  • 문제 4 : LCD 디스플레.... 최익필의 이름없는 블로그 2009
  • 궁극의 예외처리. 이름없는 블로그 2008
  • Maximum sum. 티스토리 지점 2008

글 보관함

  • 2010/03 (1)
  • 2010/02 (1)
  • 2010/01 (1)
  • 2009/12 (3)
  • 2009/08 (1)

태그목록

  • 프로그래밍
  • Iterator
  • Array
  • 플러그인
  • Freetype2
  • Inheritance
  • C
  • 쉘
  • 파일입출력
  • 한글화
  • ACM-ICPC
  • 압축
  • 탑코더
  • Sector
  • Dialog
  • 팡야
  • 클라리넷
  • 어쌔신 크리드
  • Code
  • ++i
  • Call By Reference
  • 유니코드
  • FTP
  • Codegear
  • 팡야계산기
  • 병렬처리
  • Textcube
  • Timer
  • Contest
  • OTF

지역로그 : 태그 : 방명록 : 관리자 : 글쓰기
귀차니스트’s Blog is powered by Textcube 1.7.5 : Risoluto / Designed by DesignNia.net