HDU 1612 The Blocks Problem
Information
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();
}