UVA 101

本文详细解析了一道耗时4小时完成的UVAPOJ编程题,通过具体实现展示了如何利用C语言进行堆栈操作及元素移动,包括move_onto、move_over、pile_onto和pile_over等关键函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*这题写了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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值