Uvaoj-101: Small wooden block

Source: Internet
Author: User

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

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 aren blocks on the table (numbered from 0 ton-1) with Blockbi adjacent to blockbi+1 for all as shown in The diagram below:

Figure :Initial Blocks World


The valid commands for the robot arm that manipulates blocks are:move a Ontob

Where a andb is block numbers, puts Blocka onto blockb after returning any blocks that is stacked on top of Blocksa andb To their initial positions.

Move a Overb

Where a andb is block numbers, puts Blocka onto the top of the stack containing BLOCKB, after returning any blocks that a Re stacked on top of Blocka to their initial positions.

Pile a Ontob

Where a andb is block numbers, moves the pile of blocks consisting of Blocka, and any blocks that is stacked above block A, onto blockb. All blocks on top of BLOCKB is moved to their initial positions prior to the pile taking place. The blocks stacked above Blocka retain their order when moved.

Pile a Overb

Where a andb is block numbers, puts the pile of blocks consisting of Blocka, and any blocks that is stacked above Blocka , onto the top of the stack containing blockb. The blocks stacked above Blocka retain their original order when moved.

Quit

Terminates manipulations in the block world.

Any command in which a =b, or in Whicha andb be 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 Input

The input begins with an integern to 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 Thequit 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 Numberedi (Wheren are 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 be one line of output for each block position (i.e.,n lines of output wheren are the integer on the first line of input).

Sample Input

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:


Problem Solving: Simulation questions, Rujia 110 pages of examples. Yes, I just hit the code on the Rujia book, because the focus of this exercise is on the use of vectors. Although the problem is not difficult, but it took me a long time to finish, because of some small details, the code I made a certain comment. Big God, please turn right, and I like the side of the same side of the code I will be the new master of today is only a small record;


Code

#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace Std;
const int maxn=30;
int n;
Vector <int> PILE[MAXN];

void Find_block (int a,int &p,int &h)//The reference is returned when the changed p and H values are returned;
{
for (p=0; p<n; p++)//Be careful not to redefine P and h in the loop;
{
int len=pile[p].size ();//can also be placed in the loop, but on the outside can be a little bit faster;
for (h=0; h<len; h++)
{
printf ("p = =%d\nh = =%d\n", p,h);
if (pile[p][h]==a)
return;//void function can be returned in this way, posture +1;
}
}
}
void Clear_above (int p,int h)//clear the block on a or b;
{
for (int i=h+1; i<pile[p].size (); i++)
{
int temp=pile[p][i];
Pile[temp].push_back (temp);//put back the original position;
}
Pile[p].resize (h+1);//redefine indefinite long array size;
}
void Pile_onto (int p,int h,int p2)//place The block A and a above the top of B;
{
for (int i=h; i<pile[p].size (); i++)
{
Pile[p2].push_back (Pile[p][i]);
}
Pile[p].resize (h);
}
void print ()
{
for (int i=0; i<n; i++)
{
printf ("%d:", i);
for (int j=0; j<pile[i].size (); j + +)
{
printf ("%d", pile[i][j]);
}
printf ("\ n");
}

}
int main ()
{
int A, B;
cin>>n;
String s1,s2;
for (int i=0; i<n; i++)
{
Pile[i].push_back (i);
printf ("pile[%d][0] = =%d\n", i,pile[i][0]);
}
while (cin>>s1&&s1!= "quit" &&cin>>a>>s2>>b)
{
int PA,PB,HA,HB;
cout<< "S1 = =" <<s1<< "s2 = =" <<s2<<endl;//string can not be printed with%s;
Find_block (A,pa,ha);//printf ("pa = =%d\nha =%d\n", pa,ha);
Find_block (B,PB,HB);//printf ("PB = =%D\NHB =%d\n", PB,HB);
if (PA==PB)
Continue
if (s2== "onto") Clear_above (PB,HB);
if (s1== "move") Clear_above (Pa,ha);
Pile_onto (PA,HA,PB);
}
Print ();
return 0;
}


Notes:

Vector ' s function:

Vector <int> Vec;

Vec.push_back (); trailing insert element;

Vec.pop_back (); tail delete element;

The length of the array at the same time as the element is inserted and deleted varies;

Vec[]; Use subscript to access elements;

Vec.insert (Vec.begin () +i,a); Insert a at the front of the i+1 element;

Vec.erase (Vec.begin () +2); Delete the third element;

Vec.size (); Gets the indefinite length array size;

Vec.clear (); empty indefinite long array;

Reverse (Vec.begin (), Vec.end ()), flip indefinite long array;

Sort (Vec.begin (), Vec.end (), CMP); Sorts a long array of patches;

Vec.resize (n,0); Reinitialize an indefinite long array to an indefinite array of 0 for each element of length n;

Vec.resize (n); re-delimits the length of the indefinite long array to n;


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.