/*这题写了4个小时快....哭了....确保UVA POJ均可AC*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int block[100][100];
int N, top[100];
void clear( int x, int y )
{
int i, s;
for( i = top[x]-1; i >= y; i-- )
for( s = 0; s < N; s++ )
if( block[x][i] == s )
block[s][top[s]++] = block[x][i];
top[x] = y;
}
void init()
{
int i;
memset(block, 0, sizeof(block) );
memset( top, 0, sizeof(top) );
for( i = 0; i < N; i++ )
block[i][0] = i;
for( i = 0; i < N; i++ )
top[i] = 1;
}
int search( int num )
{
int i, j;
for( i = 0; i < N; i++ )
for( j = 0; j < top[i]; j++ )
if( block[i][j] == num )
return i*10+j;
}
int tr_stack( int num )
{
return num /= 10;
}
int tr_top( int num )
{
return num %= 10;
}
void move_onto( int a, int b )
{
int a_sta, a_top, b_sta, b_top;
a_sta = tr_stack( search( a ) );
a_top = tr_top( search( a ) );
b_sta = tr_stack( search( b ) );
b_top = tr_top( search( b ) );
if( a_sta == b_sta )
return;
clear( a_sta, a_top+1 );
clear( b_sta, b_top+1 );
block[b_sta][top[b_sta]++] = a;
top[a_sta]--;
}
void move_over( int a, int b )
{
int a_sta, a_top, b_sta, b_top;
a_sta = tr_stack( search( a ) );
a_top = tr_top( search( a ) );
b_sta = tr_stack( search( b ) );
if( a_sta == b_sta )
return;
clear( a_sta, a_top+1 );
block[b_sta][top[b_sta]++] = a;
top[a_sta]--;
}
void pile_onto( int a, int b )
{
int a_sta, a_top, b_sta, b_top, i;
b_sta = tr_stack( search( b ) );
b_top = tr_top( search( b ) );
a_sta = tr_stack( search( a ) );
a_top = tr_top( search( a ) );
if( a_sta == b_sta )
return;
clear( b_sta, b_top+1 );
for( i = a_top; i < top[a_sta]; i++ )
block[b_sta][top[b_sta]++] = block[a_sta][i];
top[a_sta] = a_top;
}
void pile_over( int a, int b )
{
int a_sta, a_top, b_sta, b_top, i;
a_sta = tr_stack( search( a ) );
a_top = tr_top( search( a ) );
b_sta = tr_stack( search( b ) );
b_top = tr_top( search( b ) );
if( a_sta == b_sta )
return ;
for( i = a_top; i < top[a_sta]; i++ )
block[b_sta][top[b_sta]++] = block[a_sta][i];
top[a_sta] = a_top;
}
int main()
{
int i, j, num1, num2;
char str1[100], str2[100];
scanf( "%d\n", &N );
init();
while( scanf( "%s%d%s%d\n", str1, &num1, str2, &num2 ) )
{
if( str1[0] == 'q' )
{
for( i = 0; i < N; i++ )
{
printf( "%d:", i );
for( j = 0; j < top[i]; j++ )
printf( " %d", block[i][j] );
printf( "\n" );
}
break;
}
if( str1[0] == 'm' && str2[1] == 'n' )
{
if( num1 == num2 )
continue;
move_onto( num1, num2 );
}
if( str1[0] == 'm' && str2[1] == 'v' )
{
if( num1 == num2 )
continue;
move_over( num1, num2 );
}
if( str1[0] == 'p' && str2[1] == 'n' )
{
if( num1 == num2 )
continue;
pile_onto( num1, num2 );
}
if( str1[0] == 'p' && str2[1] == 'v' )
{
if( num1 == num2 )
continue;
pile_over( num1, num2 );
}
}
return 0;
}
UVA 101
最新推荐文章于 2024-02-26 12:33:46 发布