uva 101 the blocks problem

uva老不让过……
注意:

  • move a onto b

where a and b areblock numbers, puts block a onto block b afterreturning any blocks that are stacked on top of blocks a and b to their initial positions.


to their initial positions.是指一开始的位置,即4号石块回到第4号栈。

Any command in which a = b orin which a and b are in the same stack ofblocks is an illegal command. All illegal commands should be ignored and shouldhave no affect on the configuration of blocks.

这个千万要小心,一不留神会造成程序runtime error,这种非法情况直接忽略。不忽略的话,小心堆成一个很高很高的栈……

效率情况:

Total Submissions Users that tried it Users that solved it
65347 13675 8497
Your best accepted try
Ranking Submission Run Time Language Submission Date
490 9624560 0.008 C++ 2012-01-07 05:20:01

读题相当重要,比刷牙还重要。不把题目读得精透,千万不要急着下手……

#include<stdio.h>
const int maxn=30;

int s[maxn][maxn],top[maxn],pos[maxn],t[maxn],t_top,n;

void init()
{
	for(int i=0;i<n;i++)
	{
		s[i][1]=i;
		top[i]=1;
		pos[i]=i;
	}
}

void move(int p,int x)
{
	while(s[p][top[p]]!=x)
	{
		int a=s[p][top[p]--];
		s[a][++top[a]]=a;
		pos[a]=a;
	}
}

void print_ans()
{
	for(int i=0;i<n;i++)
	{
		printf("%d:",i);
		for(int j=1;j<=top[i];j++)
		  printf(" %d",s[i][j]);
  		printf("\n");
	}
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("101.txt","r",stdin);
#endif
	scanf("%d",&n);
	init();
	char s1[5],s2[5];
	int a,b;
	while(scanf("%s%d%s%d",s1,&a,s2,&b)==4)
	{
		if(a==b || pos[a]==pos[b]) continue;
		if(s1[0]=='m')
		{
			move(pos[a],a);
			top[pos[a]]--;
			pos[a]=pos[b];
			if(s2[1]=='n')
			{
				move(pos[b],b);
				s[pos[b]][++top[pos[b]]]=a;
			}
			else s[pos[b]][++top[pos[b]]]=a;
		}
		else
		{
			t_top=0;
			while(1)
			{
				t[++t_top]=s[pos[a]][top[pos[a]]--];
				pos[t[t_top]]=pos[b];
				if(t[t_top]==a) break;
			}
			if(s2[1]=='n')
				move(pos[b],b);
			while(t_top)
				s[pos[b]][++top[pos[b]]]=t[t_top--];
		}
		//printf("%s %d %s %d\n",s1,a,s2,b);
		//print_ans();
	}
	print_ans();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值