The Blocks Problem |
In thisproblem you will model a simple block world under certain rules andconstraints. 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 afterreturning any blocks that are stacked on top of blocks a and b totheir initial positions.
where a and b are block numbers, puts block a onto the top of thestack containing block b, after returning any blocks that are stackedon top of block a to their initial positions.
where a and b are block numbers, moves the pile of blocks consistingof block a, and any blocks that are stacked above block a, ontoblock b. All blocks on top of block b are moved to their initialpositions prior to the pile taking place. The blocks stacked above blocka retain their order when moved.
where a and b are block numbers, puts the pile of blocks consistingof block a, and any blocks that are stacked above block a, ontothe top of the stack containing block b. The blocks stacked above blocka retain their original order when moved.
terminates manipulations in the block world.
Any command in which a = b or in which a and bare in the same stack of blocks is an illegal command. All illegalcommands should be ignored and should have noaffect on the configuration of blocks.
The number of blocks is followed by a sequence of block commands, onecommand per line. Yourprogram should process all commands until the quit command isencountered.
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. Eachoriginal block position numbered i (where n is the number of blocks) should appearfollowed immediately by a colon.If there is at least a blockon 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:
#include<iostream> #include<stdio.h> #include<string> #include<stack> using namespace std; int main () { stack <int> sta[100]; int t; int num[100]; int res[100]; string m1,m2; int p1,p2; cin >> t; getchar(); for( int i = 0; i < t ;i++) { sta[i].push(i); num[i] = i; } while(1) { cin >> m1; if(m1 == "quit") break; cin >>p1 >>m2 >>p2; if (num[p1] == num[p2]) continue; if (m1 == "move" && m2 == "over") { for (;sta[num[p1]].top() != p1; ) { sta[ sta[num[p1]].top() ].push(sta[num[p1]].top()); num[sta[num[p1]].top()] = sta[num[p1]].top(); sta[num[p1]].pop(); } sta[num[p2]].push(p1); sta[num[p1]].pop(); num[p1] = num[p2]; } if (m1 == "pile" && m2 == "over") { int k = 0; int temp[200]; for (;sta[num[p1]].top() != p1; ) { temp[k++] = sta[num[p1]].top(); num[sta[num[p1]].top()] = num[p2]; sta[num[p1]].pop(); } sta[num[p1]].pop(); temp[k] = p1; num[p1] = num[p2]; for(int w = k ;w >= 0; w--) sta[num[p2]].push(temp[w]); } if (m1 == "move" && m2 == "onto") { for (;sta[num[p1]].top() != p1; ) { sta[ sta[num[p1]].top() ].push(sta[num[p1]].top()); num[sta[num[p1]].top()] = sta[num[p1]].top(); sta[num[p1]].pop(); } for (;sta[num[p2]].top() != p2; ) { sta[ sta[num[p2]].top() ].push(sta[num[p2]].top()); num[sta[num[p2]].top()] = sta[num[p2]].top(); sta[num[p2]].pop(); } sta[num[p2]].push(sta[num[p1]].top()); sta[num[p1]].pop(); num[p1] = num[p2]; } if (m1 == "pile" && m2 == "onto") { int k = 0; int temp[200]; for (;sta[num[p1]].top() != p1; ) { temp[k++] = sta[num[p1]].top(); num[sta[num[p1]].top()] = num[p2]; sta[num[p1]].pop(); } sta[num[p1]].pop(); temp[k] = p1; num[p1] =num[p2]; for (;sta[num[p2]].top() != p2; ) { sta[ sta[num[p2]].top() ].push(sta[num[p2]].top()); num[sta[num[p2]].top()] = sta[num[p2]].top(); sta[num[p2]].pop(); } for(int w = k ;w >= 0; w--) sta[num[p2]].push(temp[w]); } } int j; for(int i = 0;i < t;i++) { cout << i <<":"; for( j = 0 ;!sta[i].empty();j++) { res[j] = sta[i].top(); sta[i].pop(); } for (j = j -1; j >= 0;j--) cout<<" "<<res[j]; cout << endl; } return 0; }
原文地址:http://blog.csdn.net/yeyeyeguoguo/article/details/38016303