UVa 101 - The Blocks Problem(积木问题,指令操作)
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:
感觉有必要解释一下题目的大致意思:
背景
在计算机科学中的很多地方都会使用简单,抽象的方法来做分析和实验验究。比如在早期的规划学和机器人学的人工智能研究就利用一个积木世界,让机械臂执行操作积木的任务。
在这个问题中,你将在确定的规则和约束条件下构建一个简单的积木世界。这不是让你来研究怎样达到某种状态,而是编写一个“机械臂程序”来响应有限的命令集。
问题
问题就是分析一系列的命令,告诉机械臂如何操纵放在一个平台上的积木。最初平台上有n个积木(编号由0到n - 1),对于任意的0 ≤ i < n - 1,积木bi都与bi + 1相临,图示如下:
Figure: Initial Blocks World
图:积木世界的初始状态
机械臂操作积木的有效指令列举如下:
move a onto b
- a和b都是积木的编号,先将a和b上面所有的积木都放回原处,再将a放在b上。
- a和b都是积木的编号,先将a上面所有的积木放回原处,再将a放在b上。(b上原有积木不动)
- a和b都是积木的编号,将a和其上面所有的积极组成的一摞整体移动到b上。在移动前要先将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
利用二维数据求解的参考代码:
- #include <bits/stdc++.h>
- #define MAX 30
- #define INF 10000000
- using namespace std;
- int MAP[MAX][MAX];
- void find_pos(int MAP[MAX][MAX],int n,int temp,int &posx,int &posy)
- {
- bool flag=false;
- for(int i=; i<n; i++)
- {
- for(int j=; j<n; j++)
- {
- if(MAP[i][j]==temp)
- {
- posx=i;
- posy=j;
- flag=true;
- break;
- }
- if(flag==true)
- break;
- }
- }
- }
- int main()
- {
- int n,i,j;
- while(~scanf("%d",&n))
- {
- string s1,s2;
- int start,to;
- int posx,posy;
- for(i=; i<n; i++)
- {
- for(j=; j<n; j++)
- if(j==)
- MAP[i][j]=i;
- else
- MAP[i][j]=INF;
- }
- while(cin>>s1)
- {
- if(s1=="quit")
- break;
- else if(s1=="move")
- {
- cin>>start>>s2>>to;
- if(s2=="onto")
- {//判断是否在同一摞
- int x1,x2;
- find_pos(MAP,n,start,posx,posy);
- x1=posx;
- find_pos(MAP,n,to,posx,posy);
- x2=posx;
- if(x1==x2)
- continue;
- find_pos(MAP,n,start,posx,posy);
- for(j=posy+;j<n;j++)
- {
- if(MAP[posx][j]!=INF)
- {
- MAP[MAP[posx][j]][]=MAP[posx][j];
- MAP[posx][j]=INF;
- }
- }
- MAP[posx][posy]=INF;
- find_pos(MAP,n,to,posx,posy);
- for(j=posy+;j<n;j++)
- {
- if(MAP[posx][j]!=INF)
- {
- MAP[MAP[posx][j]][]=MAP[posx][j];
- MAP[posx][j]=INF;
- }
- }
- MAP[posx][posy+]=start;
- }
- else if(s2=="over")
- {
- int x1,x2;
- find_pos(MAP,n,start,posx,posy);
- x1=posx;
- find_pos(MAP,n,to,posx,posy);
- x2=posx;
- if(x1==x2)
- continue;
- find_pos(MAP,n,start,posx,posy);
- for(j=posy+;j<n;j++)
- {
- if(MAP[posx][j]!=INF)
- {
- MAP[MAP[posx][j]][]=MAP[posx][j];
- MAP[posx][j]=INF;
- }
- }
- MAP[posx][posy]=INF;
- find_pos(MAP,n,to,posx,posy);
- for(j=posy+;j<n;j++)
- {
- if(MAP[posx][j]==INF)
- {
- MAP[posx][j]=start;
- break;
- }
- }
- }
- }
- else if(s1=="pile")
- {
- cin>>start>>s2>>to;
- if(s2=="onto")
- {
- int x1,x2;
- find_pos(MAP,n,start,posx,posy);
- x1=posx;
- find_pos(MAP,n,to,posx,posy);
- x2=posx;
- if(x1==x2)
- continue;
- find_pos(MAP,n,to,posx,posy);
- for(j=posy+;j<n;j++)
- {
- if(MAP[posx][j]!=INF)
- {
- MAP[MAP[posx][j]][]=MAP[posx][j];
- MAP[posx][j]=INF;
- }
- }
- int tempx=posx,tempy=posy;
- find_pos(MAP,n,start,posx,posy);
- for(j=tempy+;j<n;j++)
- {
- for(int j2=posy;j2<n;j2++)
- {
- if(MAP[posx][j2]!=INF)
- {
- MAP[tempx][j]=MAP[posx][j2];
- MAP[posx][j2]=INF;
- break;
- }
- }
- }
- }
- else if(s2=="over")
- {
- int x1,x2;
- find_pos(MAP,n,start,posx,posy);
- x1=posx;
- find_pos(MAP,n,to,posx,posy);
- x2=posx;
- if(x1==x2)
- continue;
- find_pos(MAP,n,to,posx,posy);
- int tempx=posx,tempy=posy;
- for(j=posy;j<n;j++)
- {
- if(MAP[tempx][j]==INF)
- {
- tempy=j;
- break;
- }
- }
- find_pos(MAP,n,start,posx,posy);
- for(j=tempy;j<n;j++)
- {
- for(int j2=posy;j2<n;j2++)
- {
- if(MAP[posx][j2]!=INF)
- {
- MAP[tempx][j]=MAP[posx][j2];
- MAP[posx][j2]=INF;
- break;
- }
- }
- }
- }
- }
- }
- for(i=;i<n;i++)
- {
- printf("%d:",i);
- for(j=;j<n;j++)
- {
- if(MAP[i][j]!=INF)
- {
- printf(" %d",MAP[i][j]);
- }
- }
- printf("\n");
- }
- }
- }
其他解题思路博客推荐1:
http://www.cnblogs.com/devymex/archive/2010/08/04/1792128.html
其他解题思路博客推荐1:
http://blog.csdn.net/mobius_strip/article/details/12765319
UVa 101 - The Blocks Problem(积木问题,指令操作)的更多相关文章
- UVa 101 The Blocks Problem Vector基本操作
UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...
- UVa 101 The Blocks Problem
题意:给出从左到右放置的n块木块(从0开始编号),再给出四种操作,再给出相应的操作,输出操作结束后每一堆木块的情况. 学习的紫书,因为每一堆的木块数是在发生变化的,所以用vector. 然后就是模拟几 ...
- UVa 101 - The Blocks Problem STL
题目:给你n个方块,有四种操作: .move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面: .move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上 ...
- Uva 101 -- the block problem
Uva 101 the block problem 题目大意: 输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为0~n-1的位置.现对这些木块进行操作,操作分为四种. 1.move a o ...
- uvaoj 101 - The Blocks Problem(vector应用+技巧)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page= ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
- 【UVA - 101】The Blocks Problem(vector+模拟)
The Blocks Problem Descriptions:(英语就不说了,直接上翻译吧) 初始时从左到右有n个木块,编号为0~n-1,要求实现下列四种操作: move a onto b: 把a和 ...
- EOJ 1501/UVa The Blocks Problem
Many areas of Computer Science use simple, abstract domains for both analytical and empirical studie ...
- POJ 1208 The Blocks Problem
The Blocks Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5397 Accepted: 231 ...
随机推荐
- IE下点击scrollbar会导致焦点移动到body
现象 IE这货果然与众不同,当光标焦点在input时,点击同页面内其他区域的scrollbar,会导致焦点移动到body,从而触发绑定在input上的blur事件,如果input中的值与之前不同,甚至 ...
- 记第二次使用php开发项目之绝不重复自己
严格说起来,自己并非一个合格的php程序员.第一次使用php开发,不过是因为游戏上线,需要一个统计管理后台和GM后台,因为招聘已经来不及,所以我就上前线了! 凭着对php语法的一点点记忆(大学的时候学 ...
- js-弹出窗口
一.JS最常用三种弹出对话框 1.对话框 //弹出对话框并输出一段提示信息 function ale() { //弹出一个对话框 alert("提示信息!"); } 2.询问框 / ...
- Angular系列------AngularJS快速开始(转载)
Hello World! 开始学习AngularJS的一个好方法是创建经典应用程序“Hello World!”: 使用您喜爱的文本编辑器,创建一个HTML文件,例如:helloworld.html. ...
- C#中CookieContainer获取里面cookie值异常:InvokeMember("m_domainTable") FieldAccessException
1.可能是主机提供商的 安全问题. Their hosts works in medium trustsecurity, and ASProxy needs a full trust security ...
- LeetCode126:Word Ladder
题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...
- 无法打开Android SDK Manager的解决办法
不知道从什么时候开始,打开Android的SDK Manager.exe时,命令行窗口一闪就自动关掉了. 想更新一些Android的东西都更新不了. 查了一下,解决办法是: 环境变量的系统变量Path ...
- 泛函编程(6)-数据结构-List基础
List是一种最普通的泛函数据结构,比较直观,有良好的示范基础.List就像一个管子,里面可以装载一长条任何类型的东西.如需要对管子里的东西进行处理,则必须在管子内按直线顺序一个一个的来,这符合泛函编 ...
- Servlet-Jsp
Jsp实际就是Servlet. 我们访问Http://localhost:8080/Web/index.jsp的流程: 1 [jsp文件名].jsp转义为[jsp文件名_jsp].java,文件存储在 ...
- php中的常用数组函数(一)(比较多个数组的差集的函数们 array_diff_assoc() array_diff() array_diff_key() array_diff_ukey() array_diff_uassoc())
array_diff_assoc($arr1, $arr2, $arr3,... n); 返回:一个$arr1的副本,后续的数组中出现一个键值相同的元素,就在副本中删掉这个元素,最后返回这个副本. 如 ...