UVA-101 the Blocks problem (STL, Analog)

Source: Internet
Author: User

The Blocks problem
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

Description

BackgroundMany 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 Commands.

The problemThe 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  NBlocks on the table (numbered from 0 to  N-1) With block  b I Adjacent to block  b I+1 For all  As 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   is 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   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 a Re stacked above block   a , onto block   b . All blocks on top of block   b   is moved to their initial positions prior to th E pile taking place. The blocks stacked above block   a retain their order when moved.

  • Pile a over b

    where a and b are block numbers, puts the pile of blocks Consistin g of block a, and any blocks that is stacked above block a, onto the top of T He 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 is in the same stack of blocks was an illegal command. All illegal commands should is ignored and should has no affect on the configuration of blocks.

The InputThe 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.

The Output

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 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 is one line of output for each block position (i.e., n lines of output Where N is the integer in the first line 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:0 1:1 9 2 4 2:3: 3 4:5: 5 8 7 6 6:7: 8:9:


Miguel Revilla
2000-04-06

I hit the code in the Purple book.

#include <iostream> #include <string> #include <algorithm> #include <vector> #include < cstdio>using namespace Std;const int maxn = 30;int n;vector<int>pile[maxn];//Find the pile and height where the block is located, Returns the caller void Find_block (int x, int &p, int &h) as a reference (p = 0; p < n; p++) for (h = 0; h < pile[p].size (); h++ {if (pile[p][h] = = x) return;}} Move all wood blocks above the block H of the P heap height to the in situ void clear_above (int p, int h) {for (int i = h + 1; i < pile[p].size (); i++) {int b = pile[p][i]; Pile[b].push_back (b);}       Pile[p].resize (H + 1);  The P-Heap should only retain the subscript 0 to H element}//move the upper block height of H and the block above it to the top of the P2 heap (int p, int h, int p2) {for (int i = h; i < pile[p].size (); i++) Pile[p2].push_back (Pile[p][i]);p ile[p].resize (h);} void print () {for (int i = 0, i < n; i++) {cout << i << ': '; for (int j = 0; J < Pile[i].size (), j + +) Cout & lt;< "" << pile[i][j];cout << Endl;}} int main () {cin >> n;int A, b;string s1, s2;for (int i = 0; i < n; i++) pile[i].push_back (i); while (CIN&GT;&GT;S1) {if (S1 = = "Quit") break;cin >> a >> s2 >> b;int pa, ha, Pb, Hb;find_block (A, PA, ha); find_bl Ock (b, Pb, HB); if (PA = = PB) Continue;  Illegal directive if (S2 = = "onto") Clear_above (Pb, HB); Wow ... if (S1 = = "Move") Clear_above (PA, ha);p Ile_onto (PA, ha, pb);} Print ();}


UVA-101 the Blocks problem (STL, Analog)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.