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命令爲止。您能夠假定全部命令都是上面指定的形式。不會有語法錯誤的命令。)數組
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
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
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();
}