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();
}
相關文章
相關標籤/搜索