首先我们得看懂题目,然后用栈模拟,个人认为那个has数组开的那叫一个精华..... #include <string> #include <iostream> using namespace std ; const int maxn = 25 + 10 ; struct myStack { int top ; int num[maxn] ; } block[maxn] ; int has[maxn] ; void init( int n ) { memset( has , 0 , sizeof( has ) ) ; for( int i = 0 ; i < n ; i++ ) { block[i].top = 0 ; block[i].num[block[i].top] = i ; has[i] = i ; } return ; } void moveonto( int a , int b , int n ) { if( has[a] == has[b] || a == b ) return ; while( block[has[a]].num[block[has[a]].top] != a ) { int move = block[has[a]].num[block[has[a]].top] ; block[has[a]].top-- ; has[move] = move ; block[move].top++ ; block[move].num[block[move].top] = move ; } while( block[has[b]].num[block[has[b]].top] != b ) { int move = block[has[b]].num[block[has[b]].top] ; block[has[b]].top-- ; has[move] = move ; block[move].top++ ; block[move].num[block[move].top] = move ; } block[has[b]].top++ ; block[has[b]].num[block[has[b]].top] = block[has[a]].num[block[has[a]].top] ; block[has[a]].top-- ; has[a] = has[b] ; return ; } void moveover( int a , int b , int n ) { if( has[a] == has[b] || a == b ) return ; while( block[has[a]].num[block[has[a]].top] != a ) { int move = block[has[a]].num[block[has[a]].top] ; block[has[a]].top-- ; has[move] = move ; block[move].top++ ; block[move].num[block[move].top] = move ; } block[has[b]].top++ ; block[has[b]].num[block[has[b]].top] = block[has[a]].num[block[has[a]].top] ; block[has[a]].top-- ; has[a] = has[b] ; return ; } void pileonto( int a , int b , int n ) { if( has[a] == has[b] || a == b ) return ; while( block[has[b]].num[block[has[b]].top] != b ) { int move = block[has[b]].num[block[has[b]].top] ; block[has[b]].top-- ; has[move] = move ; block[move].top++ ; block[move].num[block[move].top] = move ; } int tem[maxn] ; memset( tem , 0 , sizeof( tem ) ) ; int count = 0 ; while( block[has[a]].num[block[has[a]].top] != a ) { tem[count++] = block[has[a]].num[block[has[a]].top] ; block[has[a]].top-- ; } tem[count++] = block[has[a]].num[block[has[a]].top] ; block[has[a]].top-- ; for( int i = count - 1 ; i >= 0 ; i-- ) { block[has[b]].top++ ; block[has[b]].num[block[has[b]].top] = tem[i] ; has[tem[i]] = has[b] ; } return ; } void pileover( int a , int b , int n ) { if( has[a] == has[b] || a == b ) return ; int tem[maxn] ; memset( tem , 0 , sizeof( tem ) ) ; int count = 0 ; while( block[has[a]].num[block[has[a]].top] != a ) { tem[count++] = block[has[a]].num[block[has[a]].top] ; block[has[a]].top-- ; } tem[count++] = block[has[a]].num[block[has[a]].top] ; block[has[a]].top-- ; for( int i = count - 1 ; i >= 0 ; i-- ) { block[has[b]].top++ ; block[has[b]].num[block[has[b]].top] = tem[i] ; has[tem[i]] = has[b] ; } return ; } int main() { int n ; while( cin >> n ) { getchar() ; init( n ) ; string op1 , op2 ; int a , b ; while( 1 ) { cin >> op1 ;if( op1 == "quit") break ; cin >> a >> op2 >> b ; if( op1 == "move" && op2 == "onto" ) moveonto( a , b , n ) ; if( op1 == "move" && op2 == "over" ) moveover( a , b , n ) ; if( op1 == "pile" && op2 == "onto" ) pileonto( a , b , n ) ; if( op1 == "pile" && op2 == "over" ) pileover( a , b , n ) ; getchar() ; } for( int i = 0 ; i < n ; i++ ) { cout << i << ":" ; for( int j = 0 ; j <= block[i].top ; j++ ) { cout << " " << block[i].num[j] ; } cout << endl ; } } return 0 ; }