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 commands.
The valid commands for the robot arm that manipulates blocks are:
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.
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.
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.
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 aretain their original order when moved.
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.
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.
The output should consist of the final state of the blocks world. Each original block position numbered i ( 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).
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:
题意:有n块砖块。编号为0到n-1。一开始的时候按照次序排放好。
然后一共有四个操作:
move a onto b
将砖块a和b上面的其他砖块全部返回到一开始的位置。然后将a放到b的上方
move a over b
将a上面的全部砖块返回,然后把a放到b砖块所在的位置上方
pile a onto b
将b上面的全部砖块返回,然后将a以及a以上所有的砖块一起放到b上面
pile a over b
将a以及上面的砖块放到b所在的堆的最上面
想法:每一个操作都是对栈的操作,开一个数组来记录每一堆的情况,然后用一个pos数组来记录每一块砖块的摆放位置。具体实现见代码
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<stack> #define maxn 30 using namespace std; stack<int> s[maxn]; int pos[maxn]; int main () { int n,i,j,temp,a,b; char st1[10],st2[10]; cin>>n; for (i=0; i<n; i++) { s[i].push(i); pos[i]=i; } while(cin>>st1) { if (strcmp(st1,"quit")==0) break; cin>>a>>st2>>b; int flag1,flag2; if (a==b || pos[a]==pos[b]) ; else { if (strcmp(st1,"move")==0) flag1=1; else flag1=2; if (strcmp(st2,"onto")==0) flag2=1; else flag2=2; if (flag1==1) { while(s[pos[a]].top()!=a) { temp=s[pos[a]].top(); s[pos[a]].pop(); s[temp].push(temp); pos[temp]=temp; } } if (flag2==1) { while(s[pos[b]].top()!=b) { temp=s[pos[b]].top(); s[pos[b]].pop(); s[temp].push(temp); pos[temp]=temp; } } if (flag1==1) { s[pos[a]].pop(); s[pos[b]].push(a); pos[a]=pos[b]; } if (flag1==2) { stack<int> st; while(s[pos[a]].top()!=a) { temp=s[pos[a]].top(); s[pos[a]].pop(); st.push(temp); pos[temp]=pos[b]; } s[pos[a]].pop(); st.push(a); pos[a]=pos[b]; while(!st.empty()) { temp=st.top(); st.pop(); s[pos[b]].push(temp); } } } } for (i=0; i<n; i++) { printf("%d:",i); int len=s[i].size(); while(len--) { cout<<" "<<*(&s[i].top()-len); } cout<<endl; } return 0; }