Title Link: http://poj.org/problem?id=1208The Blocks problem
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 5004 |
|
Accepted: 2119 |
Description
Many areas of computer science with simple, abstract domains for both analytical and empirical studies. For example, an early AI study of Planning and Robotics (STRIPS) used a block world in which a robot arm performed tasks I Nvolving the manipulation of blocks.
In this problem you'll model a simple block world under certain rules and constraints. Rather than determine how to achieve a specified state, you'll "program" a robotic arm to respond to a limited set of CO Mmands.
The problem is to parse a series of commands, instruct a robot arm in what to manipulate blocks that lie on a flat tabl E. Initially there is n blocks on the table (numbered from 0 to n-1) with block bi adjacent to block bi+1 for all 0 <= I < n-1 as shown in the diagram below:
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 is block numbers, puts block a onto the top of the stack containing block B, after returning any blocks tha T is stacked on top of the block A to their initial positions.
Pile a onto B
Where A and b is block numbers, moves the pile of blocks consisting of block A, and any blocks that is stacked above blo CK A, onto block B. All blocks on top of Block B is 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 bloc K 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 were in the same stack of blocks is an illegal command. All illegal commands should is ignored and should has no affect on the configuration of blocks.
Input
The input begins with an integer n on a line by itself representing the number of blocks in the block world. Assume that 0 < n < 25.
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.
Output
The output should consist of the final state of the blocks world. Each original block position numbered I (0 <= i < n where n is the number of blocks) should appear followed Immedia Tely 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 was the integer on the first Li NE of input).
Sample Input
10move 9 onto 1move 8-1move 7 over 1move 6-1pile 8 over 6pile 8-5move 2 over 1move 4-9quit
Sample Output
0:01:1 9 2 42:3: 34:5: 5 8 7 66:7:8:9:
Solution: A simulation problem, there is a small skill to learn, input a total of 4 kinds of instructions, but if completely independent processing of various instructions, the code will become lengthy and error-prone, the better way is to extract the common points between the instructions, write the function
Reduce duplicate code
Because the height of the block is uncertain, so it's good to use a vector.
Tip: You can assign a value between vectors or as a function's return value
Code:
1#include <cstdio>2#include <string>3#include <vector>4#include <iostream>5 using namespacestd;6 Const intMAXN = -;7 intN;8vector<int>PILE[MAXN];9 //find the pile and height of block A, return the caller in the form of a referenceTen voidFind_block (intAint& P,int&h) One { A for(P =0; P < n;p + +) - for(h =0; H < pile[p].size (); h++) - if(pile[p][h]==a)return; the } - //move all blocks of wood above the block height h to the original position - voidClear_above (intPinth) - { + for(inti = h +1; I < pile[p].size (); i++) - { + intb =Pile[p][i]; APile[b].push_back (b);//Put the block B back in position . at } -Pile[p].resize (H +1);//pile only the elements labeled 0~h are preserved - } - //Move the block above the height of the p stack to the top of the P2 heap. - voidPile_onto (intPintE Aintp2) - { in for(inti = h; I < pile[p].size (); i++) - Pile[p2].push_back (Pile[p][i]); to pile[p].resize (h); + } - voidprint () the { * for(inti =0; i < n; i++) $ {Panax Notoginsengprintf"%d:", i); - for(intj =0; J < Pile[i].size (); j + +) printf ("%d", Pile[i][j]); theprintf"\ n"); + } A } the intMain () + { - intA, B; $Cin>>N; $ strings1, S2; - for(inti =0; i < n; i++) Pile[i].push_back (i); - while(cin>>s1>>a>>s2>>b) the { - intPA, Pb, ha, HB;Wuyi Find_block (a,pa,ha); the Find_block (B,PB,HB); - if(PA==PB)Continue;//Illegal Instructions Wu if(s2=="onto") Clear_above (PB,HB); - if(s1=="Move") Clear_above (pa,ha); About Pile_onto (PA,HA,PB); $ } - print (); - return 0; -}
The Blocks problem (vector)