uva 101 The Blocks Problem

原题:
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 are n blocks on the table (numbered from 0 to n-1) with block bi adjacent to block bi+1 for all 0i<n1 as shown in the diagram below:
这里写图片描述
The valid commands for the robot arm that manipulates blocks are:

move a onto b

where a and b are block numbers, puts block a onto block b after returning any blocks that are stacked on top of blocks a and b to their initial positions.

move a over b

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

pile a onto b

where a and b are block numbers, moves the pile of blocks consisting of block a, and any blocks that are stacked above block a, onto block b. All blocks on top of block b are 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 are block numbers, puts the pile of blocks consisting of block a, and any blocks that are stacked 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 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 integer n 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 the quit 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 numbered i ( 0i<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 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 where n 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:

中文翻译:(来自lucky 猫)
在早期人工智慧的領域中常常會用到機器人,在這個問題中有一支機器手臂接受指令來搬動積木,而你的任務就是輸出最後積木的情形。

一開始在一平坦的桌面上有n塊積木(編號從0到n-1)0號積木放在0號位置上,1號積木放在1號位置上,依此類推,如下圖。
这里写图片描述
機器手臂有以下幾種合法搬積木的方式(a和b是積木的編號):

move a onto b
在將a搬到b上之前,先將a和b上的積木放回原來的位置(例如:1就放回1的最開始位罝)
move a over b
在將a搬到b所在的那堆積木之上之前,先將a上的積木放回原來的位罝(b所在的那堆積木不動)
pile a onto b
將a本身和其上的積木一起放到b上,在搬之前b上方的積木放回原位
pile a over b
將a本身和其上的積木一起搬到到b所在的那堆積木之上
quit
動作結束
前四個動作中若a=b,或者a, b在同一堆積木中,那麼這樣的動作算是不合法的。所有不合法的動作應該被忽略,也就是對各積木均無改變。

Input

輸入含有多組測試資料,每組測試資料的第一列有一個正整數n(0 < n < 25),代表積木的數目(編號從0到n-1)。接下來為機器手臂的動作,每個動作一列。如果此動作為 quit ,代表此組測試資料輸入結束。你可以假設所有的動作都符合上述的樣式。請參考Sample Input。

Output

每組測試資料輸出桌面上各位置積木的情形(每個位置一列,也就是共有n列),格式請參考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
代码:

#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>>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;
}

解答:
毕设终于完事了,好久没做题了。先找了一个简单题的熟悉熟悉。
此题明显用链表操作会很方便,使用C++中的list和map。
其中map用来记录每个小方块所在堆的位置,定义一个

vector<list<int> >  block;

表示一个链表数组,然后按照题目中的要求操作即可,这里注意链表的删除操作会使迭代器失效,每次移动的时候注意把移动后的模块位置重新编辑。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值