uva 101 The Blocks Problem

本文详细解析了一段复杂代码,该代码利用堆栈和键对进行操作,展示了其在解决特定问题时的高效性和灵活性。通过实例演示,深入理解堆栈的数据结构及其在实际编程场景中的应用。

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

模拟题目,考验写代码的耐心了,代码有点长但是含金量不高,堆栈和键值对的应用

#include <stdio.h>
#include <map>
#include <stack>
#include <string.h>
using namespace std;

typedef int BLOCK_INDEX;
typedef int POSITION;
stack<BLOCK_INDEX> pos[30];
map<BLOCK_INDEX,POSITION> m;

char buffer[300];

void clear_data(int n)
{
	int i;
	m.clear();
	for(i=0; i<n; i++)
	{
		while(!pos[i].empty())
			pos[i].pop();

		pos[i].push(i);
		m[i] = i;
	}

}

void move_onto(int a, int b, int n)
{
	if(a==b || m[a]==m[b])
		return;

	if(a<0 || a>=n)
		return;

	if(b<0 || b>=n)
		return;


	int pos_a, pos_b;

	pos_a = m[a];
	while(1)
	{
		if(pos[pos_a].top() != a)
		{
			m[pos[pos_a].top()] = pos[pos_a].top();
			pos[pos[pos_a].top()].push(pos[pos_a].top());
			pos[pos_a].pop();
		}
		else
		{
			break;
		}
	}

	pos_b = m[b];
	while(1)
	{
		if(pos[pos_b].top() != b)
		{
			m[pos[pos_b].top()] = pos[pos_b].top();
			pos[pos[pos_b].top()].push(pos[pos_b].top());
			pos[pos_b].pop();
		}
		else
		{
			break;
		}
	}

	m[a] = m[b];
	pos[pos_a].pop();
	pos[pos_b].push(a);
}

void move_over(int a, int b, int n)
{
	if(a==b || m[a]==m[b])
		return;

	if(a<0 || a>=n)
		return;

	if(b<0 || b>=n)
		return;

	int pos_a, pos_b;

	pos_a = m[a];
	while(1)
	{
		if(pos[pos_a].top() != a)
		{
			m[pos[pos_a].top()] = pos[pos_a].top();
			pos[pos[pos_a].top()].push(pos[pos_a].top());
			pos[pos_a].pop();
		}
		else
		{
			break;
		}
	}

	pos_b = m[b];
	m[a] = m[b];
	pos[pos_a].pop();
	pos[pos_b].push(a);
}

void pile_onto(int a, int b, int n)
{
	if(a==b || m[a]==m[b])
		return;

	if(a<0 || a>=n)
		return;

	if(b<0 || b>=n)
		return;

	int pos_a, pos_b;

	pos_b = m[b];
	while(1)
	{
		if(pos[pos_b].top() != b)
		{
			m[pos[pos_b].top()] = pos[pos_b].top();
			pos[pos[pos_b].top()].push(pos[pos_b].top());
			pos[pos_b].pop();
		}
		else
		{
			break;
		}
	}

	stack<BLOCK_INDEX> temp;
	pos_a = m[a];
	while(!pos[pos_a].empty())
	{
		temp.push(pos[pos_a].top());
		
		if(pos[pos_a].top() == a)
		{
			pos[pos_a].pop();
			break;
		}
		else
		{
			pos[pos_a].pop();
		}
	}

	while(!temp.empty())
	{
		pos[pos_b].push(temp.top());
		m[temp.top()] = pos_b;
		temp.pop();
	}
}

void pile_over(int a, int b, int n)
{
	if(a==b || m[a]==m[b])
		return;

	if(a<0 || a>=n)
		return;

	if(b<0 || b>=n)
		return;

	int pos_a, pos_b;
	pos_a = m[a];
	pos_b = m[b];

	stack<BLOCK_INDEX> temp;
	while(!pos[pos_a].empty())
	{
		temp.push(pos[pos_a].top());

		if(pos[pos_a].top() == a)
		{
			pos[pos_a].pop();
			break;
		}
		else
		{
			pos[pos_a].pop();
		}
	}

	while(!temp.empty())
	{
		pos[pos_b].push(temp.top());
		m[temp.top()] = pos_b;
		temp.pop();
	}
}

void print_result(int n)
{
	int i;
	stack<BLOCK_INDEX> temp;

	for(i=0; i<n; i++)
	{
		printf("%d:", i);
		while(!temp.empty())
			temp.pop();

		while(!pos[i].empty())
		{
			temp.push(pos[i].top());
			pos[i].pop();
		}

		while(!temp.empty())
		{
			printf(" %d", temp.top());
			temp.pop();
		}
		printf("\n");
	}
}

int main(void)
{
	int n, i;
	int a, b;
	char t;
	

	while(scanf("%d",&n) != EOF)
	{
		fscanf(stdin, "%c", &t);
		clear_data(n);
		while(1)
		{
			
			gets(buffer);
			if(!strcmp(buffer,"quit"))
				break;

			if(strstr(buffer,"move") && strstr(buffer,"onto"))
			{
				sscanf(buffer, "move %d onto %d", &a, &b);
				move_onto(a, b, n);
				continue;
			}

			if(strstr(buffer,"move") && strstr(buffer,"over"))
			{
				sscanf(buffer, "move %d over %d", &a, &b);
				move_over(a, b, n);
				continue;
			}

			if(strstr(buffer,"pile") && strstr(buffer,"onto"))
			{
				sscanf(buffer, "pile %d onto %d", &a, &b);
				pile_onto(a, b, n);
				continue;
			}

			if(strstr(buffer,"pile") && strstr(buffer,"over"))
			{
				sscanf(buffer, "pile %d over %d", &a, &b);
				pile_over(a, b, n);
				continue;
			}
		}
		print_result(n);
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值