《算法竞赛入门经典》 例题5-2 木块问题(The Blocks Problem,UVa 101)

原题及翻译

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.
例如,早期的人工智能规划和机器人(strips)研究使用了一个块世界,在这个世界中,机器人手臂执行涉及块操作的任务。
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 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 0 ≤ i < n ? 1 as shown in the diagram below:
最初,表上有n个块(编号从0到n?1),所有0≤i<n?1的块bi与块bi+1相邻,如下图所示:
在这里插入图片描述
Initial Blocks World
初始块世界
The valid commands for the robot arm that manipulates blocks are:
操作块的机器人臂的有效命令是:
?move a onto b
?将A移到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.
如果A和B是块号,则在将堆叠在块A和B顶部的任何块返回到其初始位置后,将块A放在块B上。
?move a over b
?将A移到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.
如果A和B是块号,则将块A放在包含块B的堆栈顶部,然后将堆叠在块A顶部的任何块返回到其初始位置。
?pile a onto b
?A桩到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.
如果A和B是块号,则将由A块和堆放在A块上方的任何块组成的一堆块移动到B块上。
All blocks on top of block b are moved to their initial positions prior to the pile taking place.
B块顶部的所有块在桩发生之前移动到其初始位置。
The blocks stacked above block a retain their order when moved.
堆叠在A块上方的块在移动时保持其顺序。
?pile a over b
?A桩超过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.
如果A和B是块编号,则将由A块和A块以上的任何块组成的一堆块放在包含B块的堆栈顶部。
The blocks stacked above block a retain their original order when moved.
A块以上的块移动时保持其原始顺序。
?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.
A=B或A和B在同一组块中的任何命令都是非法命令。
All illegal commands should be ignored and should have no affect on the configuration of blocks.
应忽略所有非法命令,并且不应影响块的配置。

Input

输入
The input begins with an integer n on a line by itself representing the number of blocks in the block world.
输入以一行上的整数n开始,该整数本身表示块世界中的块数。
You may assume that 0 < n < 25.
你可以假设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.
在语法上不会有错误的命令。

Output

输出
The output should consist of the final state of the blocks world.
输出应包括块世界的最终状态。
Each original block position numbered i (0 <=i <n where n is the number of blocks) should appear followed immediately by a colon.
每个编号为i的原始块位置(0 <=i <n,其中n是块的数量)应紧跟冒号出现。
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).
每个块位置应有一行输出(即,n行输出,其中n是输入第一行的整数)。

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:

思路

1.因为每个木块堆的高度不确定,所以用vector来保存很合适;而木堆的个数不超过n,所以用一个数组来存就可以了。
2.输入一共有4种指令,但如果完全独立地处理各指令,代码就会变得冗长而且易错,更好的方法是提取出指令之间的共同点,编写函数以减少重复代码。

代码分析

1.把第p堆高度为h的木块上方的所有木块移回原位:

void clear_above (int p,int h)
{
    for(int i=h+1;i<pile[p].size();i++)
 	{
  		int b=pile[p][i];
  		pile[b].push_back(b);          
  	//把木块b放回原位
 	}
 	pile[p].resize(h+1);               
 	//pile只应保留下标0~h的元素
}

2.把第p堆高度为h及其上方的木块整体移动到p2堆的顶部:

void pile_onto(int p,int h,int p2)
{
 	for(int i=h;i<pile[p].size();i++)
 	{
  		pile[p2].push_back(pile[p][i]);
 	}
 		pile[p].resize(h);
}

3.找木块a所在的pile和height,以引用的形式返回调用者:

void find_block(int a,int& p,int& h)
{
 	for(p=0;p<n;p++)
 	{
  		for(h=0;h<pile[p].size();h++)
  		{
   			if(pile[p][h]==a) return;
  		}
 	}
}

4.最后用于打印的函数:

void print()
{
 	for(int i=0;i<n;i++)
 	{
  		printf("%d:",i);
  		for(int j=0;j<pile[i].size();j++)
  		{
   			printf(" %d",pile[i][j]);
  		}
  		printf("\n");
 	}
}

5.主函数,用于读入、处理数据和调用函数:

int main ()
{
 	int a,b;
 	cin>>n;
 	//n个木块
 	string s1,s2;
 	//声明两个字符串类型的变量,用于读入命令
 	for(int i=0;i<n;i++)
 	{//将木块全部放在初始位置上
  		pile[i].push_back(i);
 	}
 	while(cin>>s1>>a>>s2>>b)
 	{
  		int pa,pb,ha,hb;
  		find_block(a,pa,ha);
  		find_block(b,pb,hb);
  		//找到两个木块所在的位置
  		if(pa==pb) continue;
  		if(s2=="onto") clear_above(pb,hb);
  		if(s1=="move") clear_above(pa,ha);
  		pile_onto(pa,ha,pb);
 	}
 	print();
 	return 0;
}

完整代码

#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
const int maxn=30;
int n;
vector <int> pile[maxn];       //每个pile[i]是一个vector
void find_block(int a,int& p,int& h)
{//找木块a所在的pile和height,以引用的形式返回调用者
 for(p=0;p<n;p++)
 {
  for(h=0;h<pile[p].size();h++)
  {
   if(pile[p][h]==a) return;
  }
 }
}
void clear_above (int p,int h)
{//把第p堆高度为h的木块上方的所有木块移回原位
 for(int i=h+1;i<pile[p].size();i++)
 {
  int b=pile[p][i];
  pile[b].push_back(b);          //把木块b放回原位
 }
 pile[p].resize(h+1);               //pile只应保留下标0~h的元素
}
void pile_onto(int p,int h,int p2)
{//把第p堆高度为h及其上方的木块整体移动到p2堆的顶部
 for(int i=h;i<pile[p].size();i++)
 {
  pile[p2].push_back(pile[p][i]);
 }
 pile[p].resize(h);
}
void print()
{
 for(int i=0;i<n;i++)
 {
  printf("%d:",i);
  for(int j=0;j<pile[i].size();j++)
  {
   printf(" %d",pile[i][j]);
  }
  printf("\n");
 }
}
int main ()
{
 int a,b;
 cin>>n;
 string s1,s2;
 for(int i=0;i<n;i++)
 {
  pile[i].push_back(i);
 }
 while(cin>>s1>>a>>s2>>b)
 {
  int pa,pb,ha,hb;
  find_block(a,pa,ha);
  find_block(b,pb,hb);
  if(pa==pb) continue;
  if(s2=="onto") clear_above(pb,hb);
  if(s1=="move") clear_above(pa,ha);
  pile_onto(pa,ha,pb);
 }
 print();
 return 0;
}
posted @ 2019-02-08 21:07  AlexKing007  阅读(250)  评论(0编辑  收藏  举报