uva 101 - The Blocks Problem

 

题目不难,模拟类的问题,用C语言写的。

 

有些细节要注意,比如各种非法命令如何处理,题目中有明确说明:

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

 

还有貌似我用ANSI C提交不支持 // 注释,会导致编译错误。

 

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

#define MAX 30

struct Stack {
	int element[MAX];
	int size;
};

struct Pile {
	struct Stack stacks[MAX];
	int size;
};

typedef struct Index {
	int i;
	int j;
} Index;

struct Pile pile;

void printPile() {
	int i, j;
	for (i = 0; i < pile.size; i++) {
		printf("%d:", i);
		for (j = 0; j < pile.stacks[i].size; j++) {
			printf(" %d", pile.stacks[i].element[j]);

		}
		printf("\n");
	}

}

void getIndex(int BlockNum, Index* index) {
	int i, j;

	for (i = 0; i < pile.size; i++) {
		for (j = 0; j < pile.stacks[i].size; j++) {
			if (BlockNum == pile.stacks[i].element[j]) {
				index->i = i;
				index->j = j;
				break;
			}

		}
	}

}

void move(Index from, Index to) {
	pile.stacks[to.i].element[to.j] = pile.stacks[from.i].element[from.j];
	pile.stacks[to.i].size++;
	pile.stacks[from.i].size--;

}
/*
 * type is 0 when move/pile "onto" sth.
 * type is not 0 when move/pile "over" sth.
 * */
void moveNum(int fromInt, int toInt, int type) {
	Index from, to;

	getIndex(fromInt, &from);
	getIndex(toInt, &to);
	if (type == 0)
		to.j++;
	else
		to.j = pile.stacks[to.i].size;

	move(from, to);

}

void multiMove(Index from, Index to) {
	int i;
	int end = pile.stacks[from.i].size;
	for (i = from.j; i < end; i++) {
		Index tmpFrom = { from.i, i };
		Index tmpTo = { to.i, pile.stacks[to.i].size };
		move(tmpFrom, tmpTo);
	}

}

void multiMoveNum(int fromInt, int toInt) {

	Index from, to;
	getIndex(fromInt, &from);
	getIndex(toInt, &to);
	multiMove(from, to);

}

void returnBlocks(Index index) {
	int i, value;
	int size = pile.stacks[index.i].size;
	for (i = index.j + 1; i < size; i++) {
		value = pile.stacks[index.i].element[i];
		Index from = { index.i, i };
		Index to = { value, pile.stacks[value].size };
		move(from, to);
	}

}

void returnNum(int blockNum) {
	Index index;
	getIndex(blockNum, &index);
	returnBlocks(index);

}

int main() {
	int n;
	int i;

	while (scanf("%d", &n) != EOF) {
		pile.size = n;
		for (i = 0; i < pile.size; i++) {
			pile.stacks[i].element[0] = i;
			pile.stacks[i].size = 1;
		}

		char cmd1[10],cmd2[10];
		int fromInt,toInt;
		while(1) {
			scanf("%s",cmd1);
			if(strcmp(cmd1,"quit")==0) {
				printPile();
				break;
			}
			scanf("%d%s%d",&fromInt,cmd2,&toInt);

			if(fromInt==toInt)
			continue;

			Index from,to;
			getIndex(fromInt,&from);
			getIndex(toInt,&to);

			if(from.i==to.i)
			continue;

			if(strcmp(cmd1,"move")==0) {
				if(strcmp(cmd2,"onto")==0) {
					returnNum(fromInt);
					returnNum(toInt);
					moveNum(fromInt, toInt,0);
				}
				else {
					returnNum(fromInt);
					moveNum(fromInt, toInt,1);

				}

			}
			else {
				if(strcmp(cmd2,"onto")==0) {
					returnNum(toInt);
					multiMoveNum(fromInt,toInt);
				}
				else {
					multiMoveNum(fromInt,toInt);
				}

			}

		}

	}

	return 0;
}

 

你可能感兴趣的:(block)