-
#include <iostream>
-
#include <deque>
-
#include <string>
-
-
using namespace std;
-
-
deque< int > *table;
-
int *link;
-
int BlockNum;
-
-
void returning( int index );
-
void moving( int index1, int index2 );
-
void piling( int index1, int index2 );
-
void print();
-
-
int main( int argc, char **argv )
-
{
-
cin >> BlockNum;
-
if( 0 < BlockNum && BlockNum < 25 ) {
-
table = new deque< int >[ BlockNum ];
-
link = new int[ BlockNum ];
-
for( int i = 0; i < BlockNum; i++ ) {
-
link[ i ] = i;
-
table[ i ].push_back( i );
-
}
-
string cmd1;
-
int a;
-
string cmd2;
-
int b;
-
while( true ) {
-
cin >> cmd1;
-
if( cmd1 == "quit" ) {
-
print();
-
break;
-
}
-
cin >> a >> cmd2 >> b;
-
if( a != b && link[ a ] != link[ b ] && 0 <= a && a < BlockNum && 0 <= b && b < BlockNum ) {
-
if( cmd1 == "move" ) {
-
if( cmd2 == "onto" ) {
-
// move A onto B
-
-
returning( a );
-
returning( b );
-
moving( a, b );
-
}
-
else if( cmd2 == "over" ) {
-
// move A over B
-
returning( a );
-
moving( a, b );
-
}
-
}
-
else if( cmd1 == "pile" ) {
-
if( cmd2 == "onto" ) {
-
// pile A onto B
-
-
returning( b );
-
piling( a, b );
-
-
}
-
else if( cmd2 == "over" ) {
-
// pile A over B
-
piling( a, b );
-
}
-
}
-
}
-
}
-
delete [] table;
-
delete [] link;
-
}
-
return 0;
-
}
-
-
void returning( int index )
-
{
-
int BlockIndex = link[ index ];
-
while( table[ BlockIndex ].back() != index ) {
-
-
int Block = table[ BlockIndex ].back();
-
table[ Block ].push_back( Block );
-
table[ BlockIndex ].pop_back();
-
link[ Block ] = Block;
-
}
-
}
-
-
void moving( int index1, int index2 )
-
{
-
int a = link[ index1 ];
-
int b = link[ index2 ];
-
table[ b ].push_back( table[ a ].back() );
-
table[ a ].pop_back();
-
link[ index1 ] = link[ index2 ];
-
}
-
-
void piling( int index1, int index2 )
-
{
-
deque< int > temp;
-
int BlockIndex1 = link[ index1 ];
-
while( table[ BlockIndex1 ].back() != index1 ) {
-
temp.push_back( table[ BlockIndex1 ].back() );
-
table[ BlockIndex1 ].pop_back();
-
}
-
temp.push_back( table[ BlockIndex1 ].back() );
-
table[ BlockIndex1 ].pop_back();
-
int BlockIndex2 = link[ index2 ];
-
while( !temp.empty() ) {
-
int linkTemp = temp.back();
-
temp.pop_back();
-
table[ BlockIndex2 ].push_back( linkTemp );
-
link[ linkTemp ] = link[ index2 ];
-
}
-
}
-
-
void print()
-
{
-
deque< int >::iterator prt;
-
for( int i = 0; i < BlockNum; i++ ) {
-
cout << i << ":";
-
prt = table[ i ].begin();
-
while( prt != table[ i ].end() ) {
-
cout << " " << *prt;
-
prt++;
-
}
-
cout << endl;
-
}
-
}
댓글을 달아 주세요