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.ios

(输入以一行中的整数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).app

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

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;//返回值为一、二、三、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();
}
相关文章
相关标签/搜索