题意:1.move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面;
2.move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上面;
3.pile a onto b,把b上面的放回原来位置,然后把a所在的堆整体放到b上面;
4.pile a over b,吧a所在堆整体放到b所在堆的上面。
自己写的代码太挫了,代码美观性有待提高
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int place[25];
int stack[25][25];
int top[25];
void init_place( int a )
{
int block,id = place[a];
while ( stack[id][top[id]] != a ) {
block = stack[id][top[id] --];
place[block] = block;
stack[block][++ top[block]] = block;
}
}
int temp[25];
void pile_a_to_b( int a, int b )
{
int topt = -1,id = place[a],ID = place[b];
while ( stack[id][top[id]] != a )
temp[++ topt] = stack[id][top[id] --];
place[a] = ID;
stack[ID][++ top[ID]] = a;
top[id] --;
while ( topt >= 0 ) {
place[temp[topt]] = ID;
stack[ID][++ top[ID]] = temp[topt --];
}
}
int main()
{
int n,a,b;
char oper[5],type[5];
while ( ~scanf("%d",&n) ) {
for ( int i = 0 ; i < n ; ++ i ) {
stack[i][0] = i;
place[i] = i;
top[i] = 0;
}
while ( scanf("%s",oper) && oper[0] != 'q' ) {
scanf("%d%s%d",&a,type,&b);
if ( place[a] == place[b] )
continue;
if ( oper[0] == 'm' )
init_place( a );
if ( type[1] == 'n' )
init_place( b );
pile_a_to_b( a, b );
}
for ( int i = 0 ; i < n ; ++ i ) {
printf("%d:",i);
int now = 0;
while ( now <= top[i] )
printf(" %d",stack[i][now ++]);
printf("\n");
}
}
return 0;
}