UVa 101 The Blocks Problem

【解题思路】

本题题意比较明确,只要了解四个指令的含义即可(以下a和b都是方块的编号),可分成四个函数编写。

move a onto b:将方块a移到方块b相邻的上方,在此之前,a和b之上的方块都要按照编号移回到原来的位置。

move a over b:将方块a移到方块b所在的堆上,在此之前,a之上的方块都要按照编号移回到原来的位置。

pile a onto b:将方块a以及之上的方块顺序不变地移到方块b相邻的上方,在此之前,b之上的方块都要按照编号移回到原来的位置。

pile a over b:将方块a以及之上的方块顺序不变地移到方块b所在的堆上。

最后题目输出部分每一行的开头是没有空格的,差点被样例输出坑到。

【代码】

#include	<stdio.h>
#include	<string.h>

typedef struct tagStack
{
	int num[30];//记录该堆的方块编号
	int len;//该堆中方块的数量
	int loc;//指示该堆的编号对应的方块在哪一个堆
}Stack;
Stack stack[30];

int searchLoc(int syn) { //在堆中搜索方块syn在哪一层,返回位置
	int p;
	int loc = stack[syn].loc;
	for (p = 0; p < stack[loc].len; p ++) {
		if (stack[loc].num[p] == syn)
			break;
	}
	return p;
}

void MoveOnto(int a, int b) { //move a onto b
	//四个函数的功能大同小异,这里只解释一个函数里的功能
	if (stack[a].loc != stack[b].loc) { //a和b两个方块所在堆不同时,指令有效
		int pa, pb, loca, locb;
		pa = searchLoc(a);
		pb = searchLoc(b);
		loca = stack[a].loc;
		locb = stack[b].loc;

		for (int i = stack[locb].len-1; i > pb; i --) { //移走方块b上方的所有方块,移回原来位置
			int t = stack[locb].num[i];
			stack[t].num[stack[t].len ++] = t;
			stack[t].loc = t;
		}
		stack[locb].len = pb+1;//修改方块b所在堆的方块个数

		for (int i = stack[loca].len-1; i > pa; i --) { //移走方块a上方的所有方块,移回原来位置
			int t = stack[loca].num[i];
			stack[t].num[stack[t].len ++] = t;
			stack[t].loc = t;
		}
		stack[loca].len = pa;//修改方块a所在堆的方块个数
		
		stack[locb].num[stack[locb].len ++] = stack[loca].num[pa]; //将方块a移动方块b的顶部,b所在堆个数加1
		stack[a].loc = locb;//方块a所在堆修改为方块b的所在堆
	}
}

void MoveOver(int a, int b) { //move a over b
	if (stack[a].loc != stack[b].loc) {
		int pa, pb, loca, locb;
		pa = searchLoc(a);
		pb = searchLoc(b);
		loca = stack[a].loc;
		locb = stack[b].loc;

		for (int i = stack[loca].len-1; i > pa; i --) {
			int t = stack[loca].num[i];
			stack[t].num[stack[t].len ++] = t;
			stack[t].loc = t;
		}
		stack[loca].len = pa;

		stack[locb].num[stack[locb].len ++] = stack[loca].num[pa];
		stack[a].loc = locb;
	}
}

void PileOnto(int a, int b) { //pile a onto b
	if (stack[a].loc != stack[b].loc) {
		int pa, pb, loca, locb;
		pa = searchLoc(a);
		pb = searchLoc(b);
		loca = stack[a].loc;
		locb = stack[b].loc;

		for (int i = stack[locb].len-1; i > pb; i --) {
			int t = stack[locb].num[i];
			stack[t].num[stack[t].len ++] = t;
			stack[t].loc = t;
		}
		stack[locb].len = pb+1;

		for (int i = pa; i < stack[loca].len; i ++) {
			int t = stack[loca].num[i];
			stack[locb].num[stack[locb].len ++] = t;
			stack[t].loc = locb;
		}
		stack[loca].len = pa;
	}
}

void PileOver(int a, int b) { //pile a over b
	if (stack[a].loc != stack[b].loc) {
		int pa, pb, loca, locb;
		pa = searchLoc(a);
		pb = searchLoc(b);
		loca = stack[a].loc;
		locb = stack[b].loc;

		for (int i = pa; i < stack[loca].len; i ++) {
			int t = stack[loca].num[i];
			stack[locb].num[stack[locb].len ++] = t;
			stack[t].loc = locb;
		}
		stack[loca].len = pa;
	}
}

int main() {
	int n, a, b;
	char cmd1[10], cmd2[10];

	while (~scanf("%d", &n)) {
		for (int i = 0; i < n; i ++) {
			stack[i].num[0] = i;
			stack[i].len = 1;
			stack[i].loc = i;
		}
		while (true) {
			scanf("%s", cmd1);
			if (strcmp(cmd1, "quit") == 0) {
				break;
<span style="white-space:pre">			</span>}
			scanf("%d %s %d", &a, cmd2, &b);

			if (strcmp(cmd1, "move") == 0) 	{
				if (strcmp(cmd2, "onto") == 0)
					MoveOnto(a, b);
				else
					MoveOver(a, b);
			}
			else {
				if (strcmp(cmd2, "onto") == 0)
					PileOnto(a, b);
				else
					PileOver(a, b);
			}
		}
		for (int i = 0 ; i < n; i ++) {
			printf("%d:", i);
			for (int j = 0; j < stack[i].len; j ++)
				printf(" %d", stack[i].num[j]);
			printf("\n");
		}
	}
	return 0;
}


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页