记录编号 102257 评测结果 AA
题目名称 [UVa 101] 木块问题 最终得分 100
用户昵称 GravatarLetter zZZz 是否通过 通过
代码语言 C++ 运行时间 0.000 s
提交时间 2014-05-17 21:52:44 内存使用 0.31 MiB
显示代码纯文本
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;
ifstream fi ("uvablock.in");
ofstream fo ("uvablock.out");
class T
{
public:
	int over,under,line;
}block[30];
	
void move_onto(int a,int b)
{
	if (block[a].under!=0)
		block[block[a].under].over=block[a].over;
	if (block[a].over!=-1)
		block[block[a].over].under=block[a].under;
	if (block[b].over!=-1)
		block[block[b].over].under=a;
	block[a].over=block[b].over;
	block[b].over=a;
	block[a].line=block[b].line;
	block[a].under=b;
}
void move_over(int a,int b)
{
	while (block[b].over!=-1)
		b=block[b].over;
	//if (block[a].under!=0)
		block[block[a].under].over=block[a].over;
	if (block[a].over!=-1)
		block[block[a].over].under=block[a].under;
	block[a].line=block[b].line;
	block[a].under=b;
	block[b].over=a;
	block[a].over=-1;
}
void pile_onto(int a,int b)
{
	int l=block[b].line,k=a,q;
	while(block[a].over!=-1)
	{
		block[a].line=l;
		a=block[a].over;
		q=a;
	}
	a=k;
	if (block[a].under!=0)
		block[block[a].under].over=-1;
	if (block[b].over!=-1)
	{
		block[block[b].over].under=q;
		block[q].over=block[b].over;
	}
	block[a].under=b;
	block[b].over=a;
}
void pile_over(int a,int b)
{
	while (block[b].over!=-1)
		b=block[b].over;
	int l=block[b].line,k=a;
	while(block[a].over!=-1)
	{
		block[a].line=l;
		a=block[a].over;
	}
	a=k;
	//if (block[a].under!=0)
		block[block[a].under].over=-1;
	block[a].under=b;
	block[b].over=a;
}
int main()
{
	int n;
	string m,nn;
	fi>>n;
	for (int i=0;i<n;i++)
	{
		block[i].over=-1;
		block[i].under=0;
		block[i].line=i;
	}
	fi>>m;
	do
	{
		if (m=="move")
		{
			int a,b;
			fi>>a>>nn>>b;
			if(a!=b&&block[a].line!=block[b].line)
			{
				if (nn=="onto")
					move_onto(a,b);
				if (nn=="over")
					move_over(a,b);
			}
		}
		if (m=="pile")
		{
			int a,b;
			fi>>a>>nn>>b;
			if(a!=b&&block[a].line!=block[b].line)
			{
				if (nn=="onto")
					pile_onto(a,b);
				if (nn=="over")
					pile_over(a,b);
			}
		}
		fi>>m;
	}while (m!="quit");
	for (int i=0;i<n;i++)
	{
		fo<<i<<":";
		for (int j=0;j<n;j++)
		{
			if (block[j].line==i&&block[j].under==0)
			{
				int p=j;
				fo<<p<<" ";
				do
				{
					p=block[p].over;
					if (p!=-1)fo<<p<<" ";
					
				}while (p!=-1);
				break;
			}
		}
		fo<<endl;
	}
	return 0;
}