您的位置:首页 > 其它

uva101 The Blocks Problem

2013-08-31 18:28 381 查看


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 

 as
shown in the diagram below:
 

Figure: Initial Blocks World

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 aretain
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 (

 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:


求解过程

这道题总体来说并不难,我是看刘汝佳老师写的《算法竞赛入门》那本书上数据结构一章的后面有这道习题就拿来做了,还是比较简单的,但是!我做了很久很久,其实编程很快,一个多小时就写完而且能通过Sample,然后我就天真地提交了,可是不是WA就是Runtime Error,一直断断续续地调了三四个小时才AC,真是不容易啊!总结几点教训:1、画个图会降低错误率,我就是纯脑想错了好多地方,编完再改就麻烦了,最好一次编对!2、看见RE千万别害怕,大多是情况都不是算法效率不高的问题,千万别绞尽脑汁去提高算法效率了,大部分情况还是程序有错误,所以编一个随机程序生成测试用例,然后去跑一下程序看看,数据量大一点就会发现错误了,发现错误也别着急,再多则几组尽可能找到简单的错误样例然后去改程序。

最关键最重要的就是move()函数了,这里一定要特别特别仔细,因为一些变量在不知不觉就给修改了。还有一点就是不需要考虑还原时在原位置还有木块的情况,即不存在在i位置i木块下还有其他木块这种可能性。因为i位置为空时是不可能移进其他木块的。

代码

#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

struct pile{
int data[26];
int top;
int cp;
pile(int n){
data[0]=n;
top=1;		//top = 0 means empty
cp=n;		//cp=i means the block on this pile initially is on i pile now
}
};

vector<pile> vcp;

void move(char v, char p,int a,int b){
if (vcp[a].cp==vcp[b].cp) return;
if (v=='m'&&p=='n'){
while (vcp[vcp[a].cp].data[--vcp[vcp[a].cp].top]!=a){
pile &orpa=vcp[vcp[vcp[a].cp].data[vcp[vcp[a].cp].top]];
orpa.data[orpa.top++]=vcp[vcp[a].cp].data[vcp[vcp[a].cp].top];
orpa.cp=vcp[vcp[a].cp].data[vcp[vcp[a].cp].top];
vcp[vcp[a].cp].data[vcp[vcp[a].cp].top]=-1;
}
vcp[vcp[a].cp].data[vcp[vcp[a].cp].top]=-1;
while (vcp[vcp[b].cp].data[--vcp[vcp[b].cp].top]!=b){
pile &orpb=vcp[vcp[vcp[b].cp].data[vcp[vcp[b].cp].top]];
orpb.data[orpb.top++]=vcp[vcp[b].cp].data[vcp[vcp[b].cp].top];
orpb.cp=vcp[vcp[b].cp].data[vcp[vcp[b].cp].top];
vcp[vcp[b].cp].data[vcp[vcp[b].cp].top]=-1;
}
vcp[vcp[b].cp].top++;
vcp[vcp[b].cp].data[vcp[vcp[b].cp].top++]=a;
vcp[a].cp=vcp[b].cp;
}
else if (v=='m'&&p=='v'){
while (vcp[vcp[a].cp].data[vcp[vcp[a].cp].top-1]!=a){
vcp[vcp[a].cp].top--;
pile &orpa=vcp[vcp[vcp[a].cp].data[vcp[vcp[a].cp].top]];
orpa.data[orpa.top++]=vcp[vcp[a].cp].data[vcp[vcp[a].cp].top];
orpa.cp=vcp[vcp[a].cp].data[vcp[vcp[a].cp].top];
vcp[vcp[a].cp].data[vcp[vcp[a].cp].top]=-1;
}
vcp[vcp[a].cp].top--;
vcp[vcp[a].cp].data[vcp[vcp[a].cp].top]=-1;
vcp[vcp[b].cp].data[vcp[vcp[b].cp].top++]=a;
vcp[a].cp=vcp[b].cp;
}
else if (v=='p'&&p=='n'){
while (vcp[vcp[b].cp].data[vcp[vcp[b].cp].top-1]!=b){
vcp[vcp[b].cp].top--;
pile &orpb=vcp[vcp[vcp[b].cp].data[vcp[vcp[b].cp].top]];
orpb.data[orpb.top++]=vcp[vcp[b].cp].data[vcp[vcp[b].cp].top];
orpb.cp=vcp[vcp[b].cp].data[vcp[vcp[b].cp].top];
vcp[vcp[b].cp].data[vcp[vcp[b].cp].top]=-1;
}
int posa=0;
pile moved = vcp[vcp[a].cp];
int lie=vcp[a].cp;
while(moved.data[posa]!=a){
posa++;
}
vcp[vcp[a].cp].top=posa;
for (int i=posa;i<moved.top;i++){
vcp[lie].data[i]=-1;
vcp[vcp[b].cp].data[vcp[vcp[b].cp].top++]=moved.data[i];
vcp[moved.data[i]].cp=vcp[b].cp;
}
}
else {
int posa=0;
pile moved = vcp[vcp[a].cp];
while(moved.data[posa]!=a){
posa++;
}
int lie=vcp[a].cp;
vcp[vcp[a].cp].top=posa;
for (int i=posa;i<moved.top;i++){
vcp[lie].data[i]=-1;
vcp[vcp[b].cp].data[vcp[vcp[b].cp].top++]=moved.data[i];
vcp[moved.data[i]].cp=vcp[b].cp;
}
}

}
void printout(int n){
for (int i=0;i<n;i++){
cout<<i<<":";
for (int j=0;j<vcp[i].top;j++){
cout<<" "<<vcp[i].data[j];
}
cout<<endl;
}
}
int main(){
int n;		//n piles
cin>>n;

for (int i=0;i<n;i++){
pile np(i);
vcp.push_back(np);
}
char command1[5];
char command2[5];
char verb,prep;
int a,b;

while (cin>>command1){
if (command1[0]=='q') break;
verb=command1[0];
cin>>a>>command2>>b;
prep=command2[1];
move(verb,prep,a,b);
}
printout(n);
return 0;
}


测试用例生成程序

#include<iostream>
#include<ctime>
#include<string>
#include<fstream>

using namespace std;

int random(int max){
return ((double)rand()/RAND_MAX)*max+0.5;
}
int main(){
srand(time(NULL));
ofstream fout;
fout.open("output2.txt");
for (int i=0;i<4;i++){
int n=random(24);

cout<<n<<endl;
fout<<n<<endl;
int col=6*n;
while(col--){
int abc=random(1);
int a=random(n-1);
int b=random(n-1);
int abd=random(1);
string verb;
string prep;
if (abc==0) verb="move";
else verb="pile";
if (abd==0) prep="onto";
else prep="over";
cout<<verb<<" "<<a<<" "<<prep<<" "<<b<<endl;
fout<<verb<<" "<<a<<" "<<prep<<" "<<b<<endl;
}
cout<<"quit"<<endl;
fout<<"quit"<<endl;
}
fout.flush();
fout.close();
return 0;
}


有问题请留言哈,一起讨论交流!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM uva 程序 模拟法