UVa 101

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=37

  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 UVa 101 as shown in the diagram below:

 
UVa 101
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 (UVa 101 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:

 感觉有必要解释一下题目的大致意思:

背景

在计算机科学中的很多地方都会使用简单,抽象的方法来做分析和实验验究。比如在早期的规划学和机器人学的人工智能研究就利用一个积木世界,让机械臂执行操作积木的任务。

在这个问题中,你将在确定的规则和约束条件下构建一个简单的积木世界。这不是让你来研究怎样达到某种状态,而是编写一个“机械臂程序”来响应有限的命令集。

问题

问题就是分析一系列的命令,告诉机械臂如何操纵放在一个平台上的积木。最初平台上有n个积木(编号由0到n - 1),对于任意的0 ≤ i < n - 1,积木bi都与bi + 1相临,图示如下:


UVa 101Figure: Initial Blocks World
图:积木世界的初始状态

机械臂操作积木的有效指令列举如下:

move a onto b

  • a和b都是积木的编号,先将a和b上面所有的积木都放回原处,再将a放在b上。
move a over b
  • a和b都是积木的编号,先将a上面所有的积木放回原处,再将a放在b上。(b上原有积木不动)
pile a onto b
  • a和b都是积木的编号,将a和其上面所有的积极组成的一摞整体移动到b上。在移动前要先将b上面所有的积极都放回原处。移动的一摞积木要保持原来的顺序不变。
pile a over b
  • a和b都是积木的编号,将a和其上面所有的积极组成的一摞整体移动到b所在一摞积木的最上面一个积木上。移动的一摞积木要保持原来的顺序不变。
quit
  • 结束积木世界的操纵。
当a = b或a和b处在同一摞时,任何企图操作a和b的命令都是非法的。所有非法的命令都要忽略,且不能对当前积木的状态产生作用。

输入

输入由1个整数n开始开始,该整数独占一行,表示积木世界中的积木数量。你可以假定0 < n < 25。

从积木数量值的下一行开始是一系列的命令,每条命令独占一行。你的程序要处理所有的命令直到输入退出命令。你可以假定所有的命令都按上文所示的格式给出。不会出现语法错误的命令。

输出

以积木世界的最终状态作为输出。每一个原始积木的位置i(0 ≤ i < n,n为积木数量)后面都要紧跟一个冒号。如果至少有一个积木在该位置上,冒号后面都要紧跟一个空格,然后是该位置上所有积木编号的序列。每2个积木的编号之间以一个空格隔开。行尾不能出现多余的空格。

每个积木位置独占一行(即第一行输入的n,对应输出n行数据)。

输入示例

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

输出示例

0: 0
1: 1 9 2 4
2:
3: 3
4:
5: 5 8 7 6
6:
7:
8:
9:

解题分析:

再明白积木世界的操作规律之后,现在就是要思考一下怎样来解决这个问题。

当我看到输出结果的时候,我会想到二维数组。为什么呢?因为每次的输出都要把相对应位置的积木编号输出,总共的行数已知也就是输入时的积木个数(同时积木编号是从0开始,也是方便了二维数组的使用,不必再其中修改序号),另外的原因就是,这四种操作有着一定的规律

规律:无需考虑将积木放回原位时原位被占的情况,因为没有任何一个操作可以把积木放在空的位置上,因此也不可能出现在第i个位置上,第i个积木的下面有其它积木的情况。可将操作分为两类,一类需要清空上面的积木,一类不需要。

这样就更容易理解题目和操作。

但是在我开始向大家讲我使用二维数组的时候,希望大家自己思考使用不同的解题方法。(不得不承认二维数组实在是太麻烦了)。

那么你在有自己的想法之后,我的二维数组就是一个对过程很具体很细致的描述。你会感觉到二维数组看似很合适其实很麻烦。

下面我就开始讲一下二维数组怎么来解决这个问题。

我们倒着来想,我的结果输出需要序号同时也需要把相对应的积木编号输出来。这样积木中一摞就相当于二维数组的一行,这一点还是很清楚的。所以我们的目的就是进过一次操作之后就把二维数组的每一行进行更新。(这里不是全部行都要更新,只是更新操作中所涉及到的那些摞积木所对应的行)。一定要明白目的是什么,不然下面的分析不是很好懂。

首先我们需要把二维数组进行初始化。二维数组中所存储的是积木的编号。由于题目中给出积木的编号在25之内,所以二维数组的初始化见下图:

 

0 INF INF INF INF ...
1 INF INF INF INF ...
2 INF INF INF INF ...
3 INF INF INF INF ...
4 INF INF INF INF ...
... ... ... ... ... ...

其中的INF可以设置为大于25的数字或者负数,关键是能够起到标记该位置没有积木的作用就可以。

 然后就要进行输入操作指令。声明string类型变量s1(move 或pile),s2(onto或over).声明int类型start,to分别表示移动积木的编号。

进行指令的判断。

在进行操作之前,我们还需要一个很重要并且很简单很无脑的函数。遍历二维数组找到元素所在位置的行下标和列下标。(自己写find_pos函数来实现找位置)

【每次指令都会调用这个find_pos函数,但是由于题目数据量不是很大,所以速度还是可以的】

(1)如果是move start onto to 类型的指令:

首先需要把start和to上的所有积木全部放回原来的位置。(根据前面的分析,放回原来的位置不需要考虑有没有积木占着这个位置,因为没有积木可以移动到不是自己原来位置的空位上)。然后把start放到to上。利用之前的find_pos函数来找到start的位置,然后使用for循环把这一摞的积木都按照编号放回原来的位置,对于to也是这样的操作。最后只需要把编号为start积木放到编号为to的积木上。注意在操作过程中一定要把移动的积木后位置初始化为INF)

(2)如果是move start over to 类型指令:

按照上面的操作方法,只需要把start上面的所有积木都放回原来的位置(所谓原来的位置就是编号的积木编号一样的位置).这时候不需要移动to上面的积木。最后把start积木移动到to积木上。初始化原来start积木位置为INF.

(3)如果是pile start onto to类型指令:

按照上面的操作方法,把to积木上面的积木都初始化,然后这些位置也相应的初始化为INF。然后把start以及start上的所有积木都拿到to积木上,不能改变积木顺序。利用嵌套的for循环加上一定的判断语句很容易实现

(4)如果是pile start over to 类型指令:

需要把start以及start上的积木都拿到to积木着一摞的上面。同样需要在操作中把没有积木的位置初始化为INF。移动一摞积木到另一摞积木的方法类似上面的操作,使用嵌套的for循环加上判断语句。

【注意1】:在移动积木的过程中一定要保持没有积木的位置的值为INF.

【注意2】:由于题目要求如果操作的积木处于同一摞则忽视该操作,同时该操作对积木世界没有任何影响。所以在判定每个指令之后首先要做的就是判断start积木和to积木是否位于同一摞。方法还是调用之前的无脑函数find_pos

利用二维数据求解的参考代码:

  1 #include <bits/stdc++.h>
  2 #define MAX 30
  3 #define INF 10000000
  4 using namespace std;
  5 int MAP[MAX][MAX];
  6 void find_pos(int MAP[MAX][MAX],int n,int temp,int &posx,int &posy)
  7 {
  8     bool flag=false;
  9     for(int i=0; i<n; i++)
 10     {
 11         for(int j=0; j<n; j++)
 12         {
 13             if(MAP[i][j]==temp)
 14             {
 15                 posx=i;
 16                 posy=j;
 17                 flag=true;
 18                 break;
 19             }
 20             if(flag==true)
 21                 break;
 22         }
 23     }
 24 }
 25 int main()
 26 {
 27     int n,i,j;
 28     while(~scanf("%d",&n))
 29     {
 30         string s1,s2;
 31         int start,to;
 32         int posx,posy;
 33         for(i=0; i<n; i++)
 34         {
 35             for(j=0; j<n; j++)
 36                 if(j==0)
 37                     MAP[i][j]=i;
 38                 else
 39                     MAP[i][j]=INF;
 40         }
 41         while(cin>>s1)
 42         {
 43             if(s1=="quit")
 44                 break;
 45             else if(s1=="move")
 46             {
 47                 cin>>start>>s2>>to;
 48                 if(s2=="onto")
 49                 {//判断是否在同一摞
 50                     int x1,x2;
 51                     find_pos(MAP,n,start,posx,posy);
 52                     x1=posx;
 53                     find_pos(MAP,n,to,posx,posy);
 54                     x2=posx;
 55                     if(x1==x2)
 56                         continue;
 57                     find_pos(MAP,n,start,posx,posy);
 58                     for(j=posy+1;j<n;j++)
 59                     {
 60                         if(MAP[posx][j]!=INF)
 61                         {
 62                             MAP[MAP[posx][j]][0]=MAP[posx][j];
 63                             MAP[posx][j]=INF;
 64                         }
 65                     }
 66                     MAP[posx][posy]=INF;
 67                     find_pos(MAP,n,to,posx,posy);
 68                     for(j=posy+1;j<n;j++)
 69                     {
 70                         if(MAP[posx][j]!=INF)
 71                         {
 72                             MAP[MAP[posx][j]][0]=MAP[posx][j];
 73                             MAP[posx][j]=INF;
 74                         }
 75                     }
 76                     MAP[posx][posy+1]=start;
 77                 }
 78                 else if(s2=="over")
 79                 {
 80                     int x1,x2;
 81                     find_pos(MAP,n,start,posx,posy);
 82                     x1=posx;
 83                     find_pos(MAP,n,to,posx,posy);
 84                     x2=posx;
 85                     if(x1==x2)
 86                         continue;
 87                     find_pos(MAP,n,start,posx,posy);
 88                     for(j=posy+1;j<n;j++)
 89                     {
 90                         if(MAP[posx][j]!=INF)
 91                         {
 92                             MAP[MAP[posx][j]][0]=MAP[posx][j];
 93                             MAP[posx][j]=INF;
 94                         }
 95                     }
 96                     MAP[posx][posy]=INF;
 97                     find_pos(MAP,n,to,posx,posy);
 98                     for(j=posy+1;j<n;j++)
 99                     {
100                         if(MAP[posx][j]==INF)
101                         {
102                             MAP[posx][j]=start;
103                             break;
104                         }
105                     }
106                 }
107             }
108             else if(s1=="pile")
109             {
110                 cin>>start>>s2>>to;
111                 if(s2=="onto")
112                 {
113                     int x1,x2;
114                     find_pos(MAP,n,start,posx,posy);
115                     x1=posx;
116                     find_pos(MAP,n,to,posx,posy);
117                     x2=posx;
118                     if(x1==x2)
119                         continue;
120                     find_pos(MAP,n,to,posx,posy);
121                     for(j=posy+1;j<n;j++)
122                     {
123                         if(MAP[posx][j]!=INF)
124                         {
125                              MAP[MAP[posx][j]][0]=MAP[posx][j];
126                              MAP[posx][j]=INF;
127                         }
128                     }
129                     int tempx=posx,tempy=posy;
130                     find_pos(MAP,n,start,posx,posy);
131                     for(j=tempy+1;j<n;j++)
132                     {
133                         for(int j2=posy;j2<n;j2++)
134                         {
135                             if(MAP[posx][j2]!=INF)
136                             {
137                                 MAP[tempx][j]=MAP[posx][j2];
138                                 MAP[posx][j2]=INF;
139                                 break;
140                             }
141                         }
142                     }
143                 }
144                 else if(s2=="over")
145                 {
146                     int x1,x2;
147                     find_pos(MAP,n,start,posx,posy);
148                     x1=posx;
149                     find_pos(MAP,n,to,posx,posy);
150                     x2=posx;
151                     if(x1==x2)
152                         continue;
153                     find_pos(MAP,n,to,posx,posy);
154                     int tempx=posx,tempy=posy;
155                     for(j=posy;j<n;j++)
156                     {
157                         if(MAP[tempx][j]==INF)
158                         {
159                             tempy=j;
160                             break;
161                         }
162                     }
163                     find_pos(MAP,n,start,posx,posy);
164                     for(j=tempy;j<n;j++)
165                     {
166                         for(int j2=posy;j2<n;j2++)
167                         {
168                             if(MAP[posx][j2]!=INF)
169                             {
170                                 MAP[tempx][j]=MAP[posx][j2];
171                                 MAP[posx][j2]=INF;
172                                 break;
173                             }
174                         }
175                     }
176                 }
177             }
178         }
179         for(i=0;i<n;i++)
180         {
181             printf("%d:",i);
182             for(j=0;j<n;j++)
183             {
184                 if(MAP[i][j]!=INF)
185                 {
186                     printf(" %d",MAP[i][j]);
187                 }
188             }
189             printf("
");
190         }
191     }
192 }

 其他解题思路博客推荐1:

http://www.cnblogs.com/devymex/archive/2010/08/04/1792128.html

 其他解题思路博客推荐1:

http://blog.csdn.net/mobius_strip/article/details/12765319