【UVA - 101】The Blocks Problem(vector+模拟)

The Blocks Problem

Descriptions :( English will not say, directly on the translation of it)

Initially from left to right there are n pieces of wood, numbered 0 ~ n-1, required to achieve the following four operations:

  • move a onto b: the block over the entire back of a and b of the initial position, and then into a top b
  • move a over b: a top of the block back into the original position of all and then on top of a heap of wood where b
  • pile a onto b: the upper portion b of the block back into the original position, and then a block of a whole all of the above into the above b
  • pile a over b: The a and above all a whole block where the block b in the top of the stack

Marks the end of a set of data is "quit", if there is an illegal instruction (a and b in the same pile), it should be ignored.

Entry

Starting from a start input integer n, a line by the integer, denotes the number of building blocks in the world. You can assume that 0 <n <25. From the beginning of the next line number value of the building blocks is a series of commands, each on a separate line. Your program should deal with all commands until the input exit command. You can assume that all commands are given in the format shown above. Command syntax error does not occur.

Export

The final status of the building blocks of the world as an output. Each original building block position i (0 ≤ i <n, n is the number of blocks) to be later followed by a colon.
If at least one building block at that position, a colon must be followed by a space and then the sequence numbers of all the building blocks of this location. There is a space between each of the two blocks of numbers. End of the line an extra space can not appear. Each building block on a separate line position (i.e., the first row of the n-input, n corresponding to the output data line).
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:

Topic links: https://vjudge.net/problem/UVA-101

 

In accordance with the meaning of the questions simulate it again, because they do not know each pile a few pieces of wood, you can use vector

AC Code

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
#defineME0 (X) Memset (X, 0, the sizeof (X))
 the using  namespace STD; 
Vector < int > V [ 30 ];
 int n-;
 // find the location, where the height of the 
void FindBlock ( int A, int & P, int & H) // returns p, h address 
{
     for (P = 0 ; P <n-; P ++ )
         for (H = 0 ; H <V [P] .size (); H ++ )
             IF (V [P] [ h] == a)
                 return ; 
} 
// the heap above the p-h-th block of reducing 
voidMoveBack ( int P, int H) 
{ 
    for ( int I = H + . 1 ; I <V [P] .size (); I ++ ) 
    { 
        int T = V [P] [I]; 
        V [T] .push_back (T ); 
    } 
    V [P] .resize (H + . 1 ); 
} 
// the heap comprises p1 of h1 in order to block movement of the P2 
void a MoveOn ( int p1, int h1, int P2) 
{ 
    for ( int = h1 of I; I <V [P1] .size (); I ++ ) 
    { 
        V [P2] .push_back (V [P1] [I]);  
    }
    V [P1] .resize (h1 of);
}
//输出格式
void Cout()
{
    for(int i=0; i<n; i++)
    {
        printf("%d:",i);
        for(int j=0; j<v[i].size(); j++)
        {
            printf(" %d",v[i][j]);
        }
        cout<<endl;
    }
}
int main()
{
    cin>>n;
    for(int i=0; i<n; i++)
        v[i].push_back(i);
    int a,b;
    string s1,s2;
    while(cin>>s1)
    {
        if(s1=="quit")
            break;
        cin>>a>>s2>>b;
        int pa,pb,ha,hb;
        FindBlock(a,pa,ha);
        FindBlock(b,pb,hb);
        if(pa==pb)
            continue;
        if(s1=="move")
            MoveBack(pa,ha);
        if(s2=="onto")
            MoveBack(pb,hb);
        MoveOn(pa,ha,pb);
    }
    Cout();
}

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/10988706.html