I+1 for allAs shown in the diagram below:
Figure :Initial Blocks World
The valid commands for the robot arm, that manipulates blocks is:
- Move a onto b
Where a and b is block numbers, puts block a onto block b after returning any blocks That is stacked on top of blocks a and b to their initial positions.
- 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 is stacked on top of block a t o their initial positions.
- pile a onto b
where a and b are block numbers, moves the pile of blocks consisting of block A , and any blocks that is 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.
- Pile a over b
Where a and b is block numbers, puts the pile of blocks consisting of block a, and any blocks That is 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.
- Quit
Terminates manipulations in the block world.
Any command in which a = b or in which a and b be in the same stack of blocks was an I Llegal command. All illegal commands should is ignored and should has no affect on the configuration of blocks.
The InputThe input begins with an integerNOn a line by itself representing the number of blocks in the block world. Assume that0 <N<.The number of blocks is followed by a sequence of block commands and one command per line. Your program should process all commands until the quit command is encountered.
Assume that all commands would be the of the form specified above. There'll be no syntactically incorrect commands.
The OutputThe 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 immed Iately by a colon. If there is at least a block on it, the colon must being followed by one space, followed by a list of blocks that appear Stac Ked in this position with each block number separated from the 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 Integ Er on the first line of input).
Sample Input10move 9 onto 1move 8-1move 7 over 1move 6-1pile 8 over 6pile 8-5move 2 over 1move 4-9quit
Sample Output0:0 1:1 9 2 4 2:3: 3 4:5: 5 8 7 6 6:7: 8:9:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace STD, #include <vector> #include <map>vector<int>w[30]; int n;void clear_onto (int p,int h) {int i; For (I=h+1;i<w[p].size (); i++) {int b=w[p][i]; W[b].push_back (b); } w[p].resize (h+1);///cout<< "onto" <<ENDL;} void pile (int pa,int ha,int pb) {int i; For (I=ha;i<w[pa].size (); i++) W[pb].push_back (W[pa][i]); W[pa].resize (HA); cout<< "Pile" <<ENDL;} void Find_ (int x,int &p,int &h) {for (p=0;p<n;p++) for (H=0;h<w[p].size (); h++) if (W[p][h] ==X) return; cout<< "Find" <<ENDL;} void print () {int i,j; for (i=0; i<n; i++) {printf ("%d:", i); For (J=0;j<w[i].size (); j + +) printf ("%d", w[i][j]); printf ("\ n"); }}int Main () {int i,x,y; Char a[32],b[32]; scanf ("%d", &n); For (i=0;i<n;i++) W[I].push_back (i); int PA,PB,HA,HB; while (scanf ("%s", a), strcmp (A, "quit")!=0) {scanf ("%d%s%d", &x,b,&y); Find_ (X,pa,ha); Find_ (Y,PB,HB); if (PA==PB) continue; if (strcmp (b, "onto") ==0) Clear_onto (PB,HB); if (strcmp (A, "move") ==0) Clear_onto (Pa,ha); Pile (PA,HA,PB); } print (); return 0;}
UVA 101 Ugly Numbers