HDU 1612 The Blocks Problem

 

Information

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 commandsThe 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
The valid commands for the robot arm that manipulates blocks are:
(翻译:问题是要分析一系列命令,这些命令指示机械臂如何操作位于平台上的块。最初在表上有n个块(从0到n-1编号),对于所有0≤i<n-1,块bi与块bi + 1相邻,如下图所示。块是:)
 
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.
(其中a和b是块编号,将堆叠在块a和b顶部的所有块返回到其初始位置后,将块a放置到块b上。) 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. (其中a和b是块号,在将堆叠在块a顶部的所有块返回到其初始位置之后,将块a放到包含块b的堆栈的顶部。)
 
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.
(其中a和b是模块编号,将由模块a和堆叠在模块a上方的所有模块组成的一堆模块移动到模块b上。发生堆之前,块b顶部的所有块均移至其初始位置。堆叠在上方的块在移动时保持其顺序。)
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.
(其中a和b是块号,将由块a以及堆积在块a上方的所有块组成的块堆放到包含块b的堆栈的顶部。堆叠在块a上方的块在移动时保持其原始顺序。)
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 illegal command. All illegal commands should be ignored and should have no affect on the configuration of blocks.
(a = b或a和b在同一块堆栈中的任何命令都是非法命令。所有非法命令都应被忽略,并且对块的配置没有影响。)

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.

(输入以一行中的整数n开头,它本身代表了块世界中的块数。您可以假设0 <n <25。块数后跟一系列的块命令,每行一个命令。您的程序应处理所有命令,直到遇到quit命令为止。您可以假定所有命令都是上面指定的形式。不会有语法错误的命令。)

Output

The output should consist of the final state of the blocks world. Each original block position numbered i (0≤i<n-1 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).

(输出应包含块世界的最终状态。编号为i的每个原始块位置(0≤i<n-1,其中n是块数)应立即出现,后跟冒号。如果上面至少有一个块,则冒号后面必须有一个空格,然后是一系列堆叠在该位置的块列表,每个块编号与其他块编号之间用空格隔开。不要在行上放置任何尾随空格。每个块位置应该有一行输出(即,n行输出,其中n是输入的第一行的整数)。)

Sample Input

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

Sample Output

 0: 0
 1: 1 9 2 4
 2:
 3: 3
 4:
 5: 5 8 7 6
 6:
 7:
 8:
 9:

  非常明显的模拟题,注意一点,若是执行pile类命令时,b在a之上,则是无效命令,其他正常模拟就行,本人用了二维的vector数组来储存每一个块的信息,ac代码如下  

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#include<cstring>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

vector< vector<int> > block(25);
int a,b,T;

int read_info(){//用于获取信息的函数
	string info1,info2;
	cin>>info1;
	if(info1.find("quit")<=info1.size())
		return 0;//退出命令
	cin>>a>>info2>>b;
	if(a==b){//无效命令
		return 5;
	}
	if(info1.find("move")<=info1.size()){
		if(info2.find("onto")<=info2.size()){
			return 1;//返回值为1、2、3、4时执行不同的操作
		}
		else if(info2.find("over")<=info2.size()){
			return 2;
		}
	}
	else if(info1.find("pile")<=info1.size()){
		if(info2.find("onto")<=info2.size()){
			return 3;
		}
		else if(info2.find("over")<=info2.size()){
			return 4;
		}
	}
	else
		return 5;
}

pair<vector<int>::iterator,int> Find(int num){//设立一个对来返回被查找块的迭代器,和其所在的数组的位置
	for(int i=0;i<T;i++){
		for(vector<int>::iterator it=block[i].begin();it!=block[i].end();it++){
			if(*it==num){
				pair<vector<int>::iterator,int> k=make_pair(it,i);
				return k;
			}
		}
	}
}

void out(){//输出函数,注意格式
	for(int i=0;i<T;i++){
		if(i<10)
		putchar(' ');
		printf("%d: ",i);
		for(vector<int>::iterator it=block[i].begin();it!=block[i].end();it++){
			printf(" %d",*it);
		}
		putchar(10);
	}	
}

void act(int mod){//执行操作
	if(mod==1){//move onto
		int pos=Find(a).second;
		for(vector<int>::iterator it=Find(a).first+1;it!=block[pos].end();it++){
			block[*it].clear();
			block[*it].push_back(*it);
			block[pos].erase(it);
			it--;
		}
		pos=Find(b).second;
		for(vector<int>::iterator it=Find(b).first+1;it!=block[pos].end();it++){
			block[*it].clear();
			block[*it].push_back(*it);
			block[pos].erase(it);
			it--;
		}
		block[pos].push_back(a);
		block[a].clear();
	}
	else if(mod==2){//move over
		int pos=Find(a).second;
		for(vector<int>::iterator it=Find(a).first+1;it!=block[pos].end();it++){
			block[*it].clear();
			block[*it].push_back(*it);
			block[pos].erase(it);
			it--;
		}
		block[Find(b).second].push_back(a);
		block[a].clear();
	}
	else if(mod==3){//pile onto
		if(Find(b).second!=Find(a).second){//排除ab在同一堆时的无效操作
			int pos=Find(b).second;
			for(vector<int>::iterator it=Find(b).first+1;it!=block[pos].end();it++){
				block[*it].clear();
				block[*it].push_back(*it);
				block[pos].erase(it);
				it--;
			}
			pos=Find(a).second;
			for(vector<int>::iterator it=Find(a).first;it!=block[pos].end();it++){
				block[Find(b).second].push_back(*it);
				block[pos].erase(it);
				it--;
			}
			block[a].clear();
		}
	}
	else if(mod==4){//pile over
		if(Find(b).second!=Find(a).second){//排除无效操作
			int pos=Find(a).second;
			for(vector<int>::iterator it=Find(a).first;it!=block[pos].end();it++){
				block[Find(b).second].push_back(*it);
				block[pos].erase(it);
				it--;
			}
			block[a].clear();
		}
	}
	//out();
}

int main(){
	cin>>T;
	for(int i=0;i<T;i++){
		block[i].push_back(i);
	}
	getchar();
	while(int oper=read_info()){
		act(oper);
	}
	out();
}

   

posted @ 2020-07-19 22:55  Simon5ei  阅读(151)  评论(0编辑  收藏  举报