uvaoj-101:小木塊

Background 

Many areas of Computer Science use 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 involving the manipulation of blocks.

In this problem you will model a simple block world under certain rules and constraints. Rather than determine how to achieve a specified state, you will ``program'' a robotic arm to respond to a limited set of commands.

The Problem 

The problem is to parse a series of commands that instruct a robot arm in how to manipulate blocks that lie on a flat table. Initially there aren blocks on the table (numbered from 0 ton-1) with blockbi adjacent to blockbi+1 for all $0 \leq i < n-1$ as shown in the diagram below:

 
\begin{figure}\centering\setlength{\unitlength}{0.0125in} %\begin{picture}(2......raisebox{0pt}[0pt][0pt]{$\bullet\bullet \bullet$ }}}\end{picture}\end{figure}
Figure: Initial Blocks World


 

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

  • move a ontob

    where a andb are block numbers, puts blocka onto blockb after returning any blocks that are stacked on top of blocksa andb to their initial positions.

  • move a overb

    where a andb are block numbers, puts blocka onto the top of the stack containing blockb, after returning any blocks that are stacked on top of blocka to their initial positions.

  • pile a ontob

    where a andb are block numbers, moves the pile of blocks consisting of blocka, and any blocks that are stacked above blocka, onto blockb. All blocks on top of blockb are 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 are block numbers, puts the pile of blocks consisting of blocka, and any blocks that are 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 are in the same stack of blocks is an illegal command. All illegal commands should be ignored and should have no affect on the configuration of blocks.

The Input 

The input begins with an integern on a line by itself representing the number of blocks in the block world. You may assume that 0 <n < 25.

The number of blocks is followed by a sequence of block commands, one command per line. Your program should process all commands until thequit command is encountered.

You may assume that all commands will be of the form specified above. There will be no syntactically incorrect commands.

The Output 

The output should consist of the final state of the blocks world. Each original block position numberedi ($0 \leq i < n$ wheren is the number of blocks) should appear followed immediately by a colon. If there is at least a block on it, the colon must be followed by one space, followed by a list of blocks that appear stacked in that position with each block number separated from 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 is the integer on the first line 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:


題解:模擬題,劉汝佳書上110頁的例題。沒錯我就是照着劉汝佳書上的代碼敲的,因爲這道題鍛鍊的重點是vector的運用。雖然題不難,但是我還是花了很長一段時間才做完,因爲一些小細節,代碼上我做了一定的註釋。大神請右轉,和我一樣的小菜可以看一下代碼貼完我會對今天新掌握的只是做一個小記錄;


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)//傳引用,返回的時候將已經改變的p和h值返回;
{
    for(p=0; p<n; p++)//注意不要在循環的時候將p和h重新定義;
    {
        int len=pile[p].size();//也可以放在循環裏,但放在外面能稍微快一點點吧;
        for(h=0; h<len; h++)
        {
            //printf("p == %d\nh == %d\n",p,h);
            if(pile[p][h]==a)
            return ;//void函數可以這樣返回,姿勢+1;
        }
    }
}

void clear_above(int p,int h)//清理a或b上的木塊;
{
    for(int i=h+1; i<pile[p].size(); i++)
    {
        int temp=pile[p][i];
        pile[temp].push_back(temp);//放回原位置;
    }
    pile[p].resize(h+1);//重新定義不定長數組大小;

}

void pile_onto(int p,int h,int p2)//將a及a上邊的木塊放在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不可以用%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;
}


筆記:

vector's function:

vector <int> vec;

vec.push_back();尾部插入元素;

vec.pop_back();尾部刪除元素;

//插入和刪除元素的同時數組的長度會隨之變化;

vec[];使用下標訪問元素;

vec.insert(vec.begin()+i,a);在i+1個元素前邊插入a;

vec.erase(vec.begin()+2);刪除第三個元素;

vec.size();獲取不定長數組大小;

vec.clear();清空不定長數組;

reverse(vec.begin(),vec.end());翻轉不定長數組;

sort(vec.begin(),vec.end(),cmp);sort對補丁長數組排序;

vec.resize(n,0);將不定長數組重新初始化爲長度爲n的每個元素都爲0的不定長數組;

vec.resize(n);重新將不定長數組的長度劃爲n;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章