(STL Preliminary) Indefinite length array: vector

Source: Internet
Author: User

STL refers to the standard template Library of C + +. (Storage of some common algorithms and containers)

Vector is an indefinite long array. It "encapsulates" some of the commonly used operations within the vector type.

For example, a is a vector. 1 The operation of the element, you can read its size with a.size () ,a.resize () change its size,a.push_back () add elements to the tail,a.pop_back () deletes the last element. 2 The operation of the array is:a.clear () empty,a.empty () test is empty.

Vectors is a template class.

Its use statement:vetor<int>a or vector<double>b.

Vector<int>a an integer array similar to an int a[], and vector<string> a string array similar to a string a[].

Vectors can be assigned directly, and can be used as arguments or return values for functions.

Example 5-2

Background
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 many parts of computer science, simple, abstract methods are used to analyze and experiment. For example, in the early days of planning and robotics, AI research used a building block world, allowing the robotic arm to perform the task of manipulating 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.
In this issue, you will build a simple world of bricks under certain rules and constraints. This is not to let you study how to achieve a certain state, but rather to write a "robotic arm program" to respond to a limited set of commands.

The problem
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 as shown in the diagram below:
The problem is analyzing a series of commands that tell the picker how to manipulate the bricks placed on a platform. Initially there are n blocks on the platform (numbered from 0 to n-1), and for any 0≤i < n-1, brick bi is pro-bi + 1, as shown below:

The valid commands for the robot arm, that manipulates blocks is:
The effective instructions for the manipulator to operate the blocks are listed below:

  • 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.
    • A and B are the number of bricks, first put all the bricks on the top of A and b back, and then put a on B.
  • 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.
    • A and B are the number of bricks, first put all the bricks on a above the original, and then put a on B. (b The original building blocks do not move)
  • 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.
    • Both A and B are the number of the building blocks, and a and all of the active stacks on top of a are moved to B. Before moving, put all of the positive on top B back in place. Move a stack of bricks to keep the original order unchanged.
  • 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.
    • Both A and B are the number of the building blocks, moving a and all of its active stacks to the top of the building block where B is located. Move a stack of bricks to keep the original order unchanged.
  • Quit
    • Terminates manipulations in the block world.
    • End the manipulation of the world of bricks.

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.
Any attempt to manipulate A and B is illegal when a = B or A and B are in the same stack. All illegal commands are ignored and cannot be useful for the state of the current block.

The Input
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 input begins with an integer n, which is a single line representing the number of bricks in the building block world. You can assume 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.
Starting from the next line of the block quantity value is a series of commands, each of which is exclusive of one line. Your program will handle all commands until you enter the Exit command.

Assume that all commands would be the of the form specified above. There'll be no syntactically incorrect commands.
You can assume that all commands are given in the format shown above. No syntax error commands are present.

The Output
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 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.
The final state of the block world as the output. The position of each original building block I (0≤i < n,n as the number of bricks) is followed by a colon. If at least one of the blocks is in that position, the colon follows a space followed by a sequence of all the building blocks in that position. The number of each 2 blocks is separated by a space. There is no extra space at the end of the 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).
Each block position is exclusive of one row (that is, the first input n, corresponding to the output n rows of data).

Sample Input
Input example

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
Output example

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

Analysis
Analysis

The height of each block heap is uncertain, so it is appropriate to use vectors to preserve it, and the number of blocks is not more than N, so it is possible to use an array to save it.

1#include <cstdio>2#include <string>3#include <vector>4#include <iostream>5 using namespacestd;6 7 Const intMAXN = -;8 intN;9vector<int>pile[maxn];//each of the pile[i] is a vectorTen  One  A //find the pile and hight of block A, return the caller in the form of a reference - voidFind_block (intAint&p,int&h) - { the      for(P =0; P < n; p++) -          for(h =0; H < pile[p].size (); h++) -             if(Pile[p][h] = =a) -                 return; + } -  + //move all blocks of wood above the block height h to the original position A voidClear_above (intPinth) at { -      for(inti = h +1; I < pile[p].size (); i++) -     { -         intb =Pile[p][i]; -Pile[b].push_back (b);//Put the block B back in position . -     } inPile[p].resize (H +1);//pile only the elements of subscript 0~h should be preserved - } to  + //Move the block above the height of the p stack to the top of the P2 heap. - voidPile_onto (intPintHintp2) the { *      for(inti = h; I < pile[p].size (); i++) $ Pile[p2].push_back (Pile[p][i]);Panax Notoginseng pile[p].resize (h); - } the  + voidprintf () A { the      for(inti =0; I < n; i++) +     { -printf"%d:", i); $          for(intj =0; J < Pile[i].size (); J + +) $         { -printf"%d", Pile[i][j]); -         } theprintf"\ n"); -     }Wuyi } the  -  Wu intMain () - { About     intA, B; $CIN >>N; -     strings1, S2; -      for(inti =0; I < n; i++) - Pile[i].push_back (i); A      while(Cin >>S1) +     { the         if(S1 = ="quit") -              Break; $Cin >> a >> s2 >>b; the         intPA, Pb, ha, HB; the Find_block (A, PA, ha); the Find_block (b, Pb, HB); the         if(PA = pb)Continue;//Illegal Instructions -         if(S2 = ="onto") Clear_above (Pb, HB); in         if(S1 = ="Move") Clear_above (PA, ha); the Pile_onto (PA, ha, pb); the     } About printf (); the     return 0; the}

(STL Preliminary) Indefinite length array: vector

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.