UVA 101 Ugly Numbers

Source: Internet
Author: User

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 Table. Initially there isNblocks on the table (numbered from 0 toN-1) with blockb Iadjacent to blockbI+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 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 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 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:



#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" &LT;&LT;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" &LT;&LT;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" &LT;&LT;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

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.