UVA 101 The Blocks problem

Source: Internet
Author: User

Original question:
Background
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
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 0 \leq I 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 t

OP 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 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 St acked above block 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 STA cked 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 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.

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

The Output

The output should consist of the final state of the blocks world. Each original block position numbered I (0≤i<n 0 \leq i where n is the number of blocks) should appear followed Immedi Ately 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

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

Sample Output

0:0
1:1 9 2 4
2:
3:3
4:
5:5 8 7 6
6:
7:
8:
9:

Chinese Translation: (from Lucky cat)
Robots are often used in the field of early artificial intelligence, where a robotic arm accepts instructions to move an area of wood, and your task is to produce the final piece of wood.

At the beginning of a flat table there are n blocks (numbered from 0 to n-1) 0 of the wood placed on the 0 position, 1 of the wood placed in the position 1, according to this type of push, the following image.

The robot arm has several ways of legally moving the wood (A and B are the number of the wood):

Move a onto B
Before moving A to B, put the accumulated wood on a and b back in the original position (for example: 1 is put back to 1 of the most started move place)
Move a over B
Before moving a to the pile of wood where B is located, first put the move place on a back to the original bit (the pile of wood where B is located)
Pile a onto B
Put a itself and the accumulated wood on the B, before moving the wood above B to put it back in position
Pile A over B
Move the a itself and the accumulated wood on it to the pile of wood where B is located.
Quit
The end of the motion
In the first four movements if a=b, or a, B are in the same pile of wood, then this kind of motion is illegal. All illegal actions should be ignored, that is, no change to the wood.

Input

The input contains multiple tests, and the first column of each test item has a positive integer n (0 < n < 25), which represents the number of the accumulated wood (from 0 to n-1). Take the action of the robot arm and make a list of each motion. If this action is quit, it is the end of the test. You can assume that all movements are in accordance with the above-mentioned formula. Please refer to sample Input.

Output

Each group of tests outputs the case for each location on the table (one column per location, that is, a total of n columns), and the format of the sample Output.

Sample 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
4
Pile 0 over 1
Pile 2 over 3
Move 1 onto 3
Quit

Sample Output

0:0
1:1 9 2 4
2:
3:3
4:
5:5 8 7 6
6:
7:
8:
9:
0:0
1:
2:2
3:3 1
Code:

#include <bits/stdc++.h> using namespace std;
Vector<list<int> >block;
Map<int,int> Mark;
FStream in,out;
    void Move_onto (int a,int b) {int ia=mark[a];
    int ib=mark[b];
    if (IA==IB) return;
        for (Auto Rit=block[ia].rbegin ();(*rit)!=a;rit++) {block[*rit].push_back (*rit);
    Mark[*rit]=*rit;
        } for (Auto Rit=block[ib].rbegin ();(*rit)!=b;rit++) {block[*rit].push_back (*rit);
    Mark[*rit]=*rit;
    } while (Block[ia].back ()!=a) block[ia].pop_back ();
    while (Block[ib].back ()!=b) block[ib].pop_back ();
    Block[ia].pop_back ();
    Block[ib].push_back (a);
    Mark[a]=ib;
Mark[b]=ib;
    } void Move_over (int a,int b) {int ia=mark[a];
    int ib=mark[b];
    if (IA==IB) return;
        for (Auto Rit=block[ia].rbegin ();(*rit)!=a;rit++) {block[*rit].push_back (*rit);
    Mark[*rit]=*rit;
    } while (Block[ia].back ()!=a) block[ia].pop_back (); Block[ib].puSh_back (a);
    Block[ia].pop_back ();
Mark[a]=ib;
    } void Pile_onto (int a,int b) {int ib=mark[b];
    int Ia=mark[a];
    if (IA==IB) return;
        for (Auto Rit=block[ib].rbegin ();(*rit)!=b;rit++) {block[*rit].push_back (*rit);
    Mark[*rit]=*rit;
    } for (Auto Rit=block[ia].rbegin ();(*rit)!=a;rit++) Mark[*rit]=ib;
    while (Block[ib].back ()!=b) block[ib].pop_back ();
    Block[ib].splice (Block[ib].end (), Block[ia],find (Block[ia].begin (), Block[ia].end (), a), block[ia].end ());
Mark[a]=ib;
    } void Pile_over (int a,int b) {int ia=mark[a];
int ib=mark[b];
    cout<<ia<< "" <<ib<<endl;
    if (IA==IB) return;
    for (Auto It=find (Block[ia].begin (), Block[ia].end (), a); It!=block[ia].end (); it++) Mark[*it]=ib;
    Block[ib].splice (Block[ib].end (), Block[ia],find (Block[ia].begin (), Block[ia].end (), a), block[ia].end ());
Mark[a]=ib;
    } int main () {Ios::sync_with_stdio (false);
    int n,a,b; StriNg S1,s2;
In.open ("In.txt");
    Out.open ("OUT.txt");
        while (cin>>n) {block.clear ();
        Mark.clear ();
        for (int i=0;i<26;i++) mark[i]=i;
            for (int i=0;i<n;i++) {list<int> l{i};
        Block.push_back (l);
            } while (CIN&GT;&GT;S1) {if (s1== "quit") break;
            cin>>a>>s2>>b;
                if (s1== "move") {if (s2== "onto") Move_onto (A, b);

            Else Move_over (A, b);
                } else {if (s2== "onto") Pile_onto (A, b);
            Else Pile_over (A, b);
            }} for (int j=0;j<n;j++) {cout<<j<< ":";
          for (auto It=block[j].begin (); It!=block[j].end (); it++) cout<< "" <<*it;  cout<<endl;
}}//In.close ();
    Out.close ();
return 0; }

Answer:
Bishi finally finished, long time no problem. First find a simple topic familiar and familiar.
It is easy to use the list and map in C + + with this problem.
Where map is used to record the location of the heap for each small block, define a

vector<list<int> >  block;

Represents a list of linked arrays, and then follow the requirements of the problem, it is important to note that the delete operation of the linked list will invalidate the iterator, each time the movement of the module location after the move to re-edit.

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.