您的位置:首页 > 其它

uva 101 - The Blocks Problem

2012-07-17 16:12 357 查看



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

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:

很烦的一道模拟题,网上找到了数据发现了小错误终于ac


下面的步骤看别人的,

機器手臂有以下幾種合法搬積木的方式(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在同一堆積木中,那麼這樣的動作算是不合法的。所有不合法的動作應該被忽略,也就是對各積木均無改變

总结起来其实只有三种操作,

1.将一个积木上的所有积木放回原位

2只将一个积木移动到另一个积木上

3将这个积木及其以上的积木移动到另一个积木上

三种操作的不同组合可以实现题目要求的四种操作要求。

一个数组记录每个积木当前的位置方便查找

用结构体记录当前所有积木的位置关系,个数;

麻烦在于每次操作都要改变很多变量,

第二种移动方式可能会出现要移动的积木在中间,还要把他上面的积木下移,这种特殊情况开始没考虑到runtime error 无数次

还好找到了数据发现了错误

21

move 2 onto 1

move 3 onto 2

move 4 onto 3

move 5 over 1

pile 1 over 10

move 9 over 8

move 11 over 8

pile 3 over 8

pile 8 over 3

move 20 over 19

pile 19 over 18

pile 18 onto 15

move 15 over 3

pile 20 onto 19

pile 19 onto 18

pile 18 over 17

quit

0: 0

1:

2:

3:

4:

5:

6: 6

7: 7

8: 8 9 11 3 4 5 15

9:

10: 10 1 2

11:

12: 12

13: 13

14: 14

15:

16: 16

17: 17 18 19 20

18:

19:

20:

#include <stdio.h>

#include <string.h>

struct node

{int num,block[30];

}a[30];

int postion[30];

void turnback(int x)

{int i,j,pos,t=postion[x],T,s=0;

for (i=1;i<=a[t].num;i++)

if (a[t].block[i]==x) {pos=i;break;}

for (i=pos+1;i<=a[t].num;i++)

{T=a[t].block[i];

++a[T].num; ++s;

for (j=a[T].num;j>=2;j--)

a[T].block[j]=a[T].block[j-1];

a[T].block[1]=T;

postion[T]=T;

}

a[t].num=a[t].num-s;

};

void move1(int x,int y)

{int i,pos,t=postion[x],T=postion[y];

++a[T].num;

a[T].block[a[T].num]=x;

for (i=1;i<=a[t].num;i++)

if (a[t].block[i]==x) {pos=i;break;}

for (i=pos;i<a[t].num;i++)

a[t].block[i]=a[t].block[i+1];

--a[t].num;

postion[x]=postion[y];

}

void move2(int x,int y)

{int i,pos,t=postion[x],T=postion[y],s=0;

for (i=1;i<=a[t].num;i++)

if (a[t].block[i]==x) {pos=i;break;}

for (i=pos;i<=a[t].num;i++)

{++a[T].num; ++s;

a[T].block[a[T].num]=a[t].block[i];

postion[a[t].block[i]]=T;

}

a[t].num=a[t].num-s;

};

int main()

{char s[30],s1[30];

int i,j,n,x,y;

while (scanf("%d",&n)!=EOF)

{

for (i=0;i<n;i++)

{a[i].num=1;

a[i].block[1]=i;

postion[i]=i;

}

while (scanf("%s",&s))

{if (strcmp(s,"quit")==0) break;

scanf("%d%s%d",&x,&s1,&y);

if ((postion[x]!=postion[y])&&(x!=y))

{

if ((strcmp(s,"move")==0)&&(strcmp(s1,"onto")==0))

{turnback(x); turnback(y); move1(x,y);}

if ((strcmp(s,"move")==0)&&(strcmp(s1,"over")==0))

{turnback(x); move1(x,y);}

if ((strcmp(s,"pile")==0)&&(strcmp(s1,"onto")==0))

{turnback(y); move2(x,y);}

if ((strcmp(s,"pile")==0)&&(strcmp(s1,"over")==0))

{move2(x,y);}

}

}

for (i=0;i<n;i++)

{printf("%d:",i);

for (j=1;j<=a[i].num;j++)

printf(" %d",a[i].block[j]);

printf("\n");

}

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: