Uva101木块问题(线性表结构)

线性表101木块问题

题目

Many areas of Computer Science use simple, abstract domains(域) for both analytical(分析法) and empirical(经验依据) studies.For example, an early AI study of planning and robotics 机器人学(STRIPS) used a block world in which a robot arm performed tasks involving the manipulation(操作) of blocks.

In this problem you will model(建模) a simple block world under certain rules and constraints. Rather than determine how to achieve a specified state, you will “program” a robotic arm to respond to a limited set of commands.

The problem is to parse(解析) a series of commands that instruct(指示) a robot arm in how to manipulate blocks that lie on a flat table. Initially there are n blocks on the table (numbered from 0 to n − 1) with block bi adjacent(相邻) to block bi+1 for all 0 ≤ i < n − 1 as shown in the diagram(图示) below:

Initial Blocks World

The valid commands for the robot arm that manipulates blocks are:

• move a onto b

where a and b are block numbers, puts block a onto block b after returning(放回) any blocks that are stacked(堆叠) on top of blocks a and b to their initial positions.

• move a over b

where a and b are block numbers, puts block a onto the top of the stack(栈) containing(包含) block b, after returning any blocks that are stacked on top of block a to their initial positions.

• pile a onto b

where a and b are block numbers, moves the pile of blocks consisting of block a, and any blocks

that are stacked above block a, onto block b. All blocks on top of block b are moved to their

initial positions prior(优先) to the pile taking place. The blocks stacked above block a retain their order(保留顺序)when moved.

• pile a over b

where a and b are block numbers, puts the pile of blocks consisting of block a, and any blocks

that are stacked above block a, onto the top of the stack containing block b. The blocks stacked

above block a retain their original order(顺序) when moved.

• quit

terminates manipulations in the block world.

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

Input

The input begins with an integer n on a line by itself representing the number of blocks in the block world. You may assume that 0 < n < 25.

The number of blocks is followed by a sequence of block commands, one command per line. Your

program should process all commands until the quit command is encountered(遇到).

You may assume that all commands will be of the form specified above. There will be no syntactically(语法) incorrect commands.

Output

The output should consist of the final state of the blocks world. Each original block position numbered i (0 ≤ i < n where n is the number of blocks) should appear followed immediately by a colon. If there is at least a block on it, the colon must be followed by one space, followed by a list of blocks that appear stacked in that position with each block number separated from other block numbers by a space. Don’t put any trailing spaces on a line.

There should be one line of output for each block position (i.e., n lines of output where n is the integer on the first line of input).

Debug:

for循环多结束条件与参数顺序

Findtop找到的坐标是栈顶(无元素)需要-1

+-坐标时只需改变纵坐标,不需要横坐标

pile逻辑不清晰,8没移走

代码:

/*
10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit
*/
#define MAX 30
#include<stdio.h>
#include<string.h>
int blk[MAX][MAX];//blocks  
int n;//块数
void find(int a,int* ax,int* ay){
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(blk[i][j]==a){
				*ax=i;
				*ay=j;
				return;
			}
		}
	}
}
void returnStack(int ax,int ay){
//	printf("return:%d %d\n",ax,ay);
	for(int i=ay+1;i<n&&(blk[ax][i]!=-1);i++){
//		printf("return:%d\n",blk[ax][i]);
		blk[blk[ax][i]][0]=blk[ax][i];
		blk[ax][i]=-1;
	}
}
void findtop(int a,int* ax,int* ay){
	find(a,ax,ay);
	for(int i=*ay+1;i<n;i++){
		if(blk[*ax][i]==-1){
//			printf("top:%d %d\n",*ax,i);
			*ay=i;
			break;
		}
	}
}
void pile(int ax,int ay,int bx,int by){
//	printf("pile:%d %d %d %d\n",ax,ay,bx,by);
//	printf("first:%d %d=%d\n",ax,ay,blk[ax][ay]);
	while(blk[ax][ay]!=-1){
//		printf("pile:%d\t",blk[ax][ay]);
		blk[bx][++by]=blk[ax][ay];
		blk[ax][ay]=-1;
		ay++;
	}
//	printf("\n") ;
}
int main(){
	char pro[5],comm[5];
	int a,b,ax,bx,ay,by;
	scanf("%d",&n);
	memset(blk,-1,sizeof(blk));
	for(int i=0;i<n;i++){
		blk[i][0]=i;
	}
//	for(int i=0;i<n;i++){
//		printf("%d:",i);
//		for(int j=0;j<n&&(blk[i][j]!=-1);j++){
//			printf(" %d",blk[i][j]);
//		}
//		if(i<n-1)printf("\n");
//	}
//	printf("\n-----------------------\n");
	while(1){
		scanf("%s",pro);
		if(strcmp(pro,"quit")==0)break;
		else{
			scanf("%d%s%d",&a,comm,&b);
			find(a,&ax,&ay);
			find(b,&bx,&by);
			if(ax==bx)continue;
			if(strcmp(pro,"move")==0){
				returnStack(ax,ay);
				if(strcmp(comm,"onto")==0){
					find(b,&bx,&by);
					returnStack(bx,by);
					blk[bx][by+1]=blk[ax][ay];
					blk[ax][ay]=-1;
				}else if(strcmp(comm,"over")==0){
					findtop(b,&bx,&by);
					blk[bx][by]=blk[ax][ay];
					blk[ax][ay]=-1;
				}
			}else if(strcmp(pro,"pile")==0){
				if(strcmp(comm,"onto")==0){
					find(b,&bx,&by);
					returnStack(bx,by);
					pile(ax,ay,bx,by);
//					blk[bx][by+1]=blk[ax][ay];
//					blk[ax][ay]=-1;
				}else if(strcmp(comm,"over")==0){
					findtop(b,&bx,&by);
//					returnStack(bx,by);
					pile(ax,ay,bx,by-1);
//					blk[bx][by+1]=blk[ax][ay];
//					blk[ax][ay]=-1;
				}
			}
		}
//		for(int i=0;i<n;i++){
//			printf("%d:",i);
//			for(int j=0;j<n&&(blk[i][j]!=-1);j++){
//				printf(" %d",blk[i][j]);
//			}
//			if(i<n-1)printf("\n");
//		}
//		printf("\n-----------------------\n");
	}
	for(int i=0;i<n;i++){
		printf("%d:",i);
		for(int j=0;j<n&&(blk[i][j]!=-1);j++){
			printf(" %d",blk[i][j]);
		}
//		if(i<n-1)printf("\n");
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lagoon_lala/article/details/81121518