101UVa木块问题

#include<stdio.h>
#include<string.h>
int block[30][30];
int ptop[30];
void backtoini(int line, int from, int to);
void print(int);
int main()
{
	//
	freopen("input.txt", "r", stdin);

	int nump;
	scanf("%d", &nump);
	int from, to;
	char ope1[5];
	char ope2[5];
	memset(block, 0, sizeof(block) );
	memset(ptop, 0, sizeof(ptop) );
	for(int i = 0; i < nump;i++)
	{
		block[i][0] = i;
		ptop[i] ++;
	}
	while(scanf("%s %d %s %d", ope1, &from, ope2, &to) == 4)
	{
		if(!strcmp(ope1,"quit") ) break;
		if(from == to) continue;
		int scr[2];
		int dest[2];
		for(int i = 0; i < nump; i++)
		{
			for(int j = 0; j < ptop[i]; j++)
			{
				if(block[i][j] == from) 
				{
					scr[0] = i;
					scr[1] = j;
				}
				if(block[i][j] == to)
				{
					dest[0] = i;
					dest[1] = j;
				}
			}
		}
		if(scr[0] == dest[0]) continue;
		
		if(!strcmp("move",ope1) && !strcmp("onto",ope2) )
		{
			backtoini(scr[0], scr[1]+1, ptop[scr[0]]-1);
			backtoini(dest[0], dest[1]+1, ptop[dest[0]]-1);
			block[dest[0]][ptop[dest[0]]++] = block[scr[0]][--ptop[scr[0]]];
		}
		if(!strcmp("move", ope1) && !strcmp("over",ope2) )
		{
			backtoini(scr[0], scr[1]+1, ptop[scr[0]]-1);
			block[dest[0]][ptop[dest[0]]++] = block[scr[0]][--ptop[scr[0]]];
		}
		if(!strcmp("pile", ope1) && !strcmp("onto", ope2))
		{
			backtoini(dest[0], dest[1]+1, ptop[dest[0]]-1);
			for(int i = scr[1]; i < ptop[scr[0]]; i++)
			{
				block[dest[0]][ptop[dest[0]]++] = block[scr[0]][i];
			}
			ptop[scr[0]] -=(ptop[scr[0]]-scr[1]);
		}
		if(!strcmp("pile", ope1) && !strcmp("over", ope2) )
		{
			for(int i = scr[1]; i < ptop[scr[0]]; i++)
			{
				block[dest[0]][ptop[dest[0]]++] = block[scr[0]][i];
			}
			ptop[scr[0]] -=(ptop[scr[0]]-scr[1]);
		}
	}
	print(nump);
}
void print(int nump)
{
	for(int i = 0; i < nump;i++)
	{
		printf("%d:", i);
		for(int j = 0; j < ptop[i]; j++)
			printf(" %d", block[i][j]);
		putchar('\n');
	}	
}
void backtoini(int line, int from, int to)
{
	for(int i = from; i <= to; i++)
	{
		int temp = block[line][i];
		block[temp][0] = temp;
		ptop[temp]++;
		ptop[line]--;
	}
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值