《算法竞赛入门经典》 例题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;
}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
这里提供一种基于数组实现的简单的木块移动的 C 语言代码: ```c #include <stdio.h> #define BLOCK_NUM 10 // 木块数量 void print_blocks(int blocks[]) { for (int i = 0; i < BLOCK_NUM; i++) { printf("%d ", blocks[i]); } printf("\n"); } int main() { int blocks[BLOCK_NUM]; // 初始化木块数组 for (int i = 0; i < BLOCK_NUM; i++) { blocks[i] = i + 1; } print_blocks(blocks); // 打印初始状态 int from, to; // 移动的起点和终点 while (scanf("%d %d", &from, &to) == 2) { // 首先找到起点 int i; for (i = 0; i < BLOCK_NUM; i++) { if (blocks[i] == from) { break; } } // 找到终点 int j; for (j = 0; j < BLOCK_NUM; j++) { if (blocks[j] == to) { break; } } // 将起点上面的木块全部移回到原位 for (int k = i + 1; k < BLOCK_NUM; k++) { blocks[k - 1] = blocks[k]; } // 将起点放到终点上面 blocks[j - 1] = from; print_blocks(blocks); // 打印移动后的状态 } return 0; } ``` 在这个程序中,我们首先定义了一个常量 `BLOCK_NUM` 来表示木块的数量。然后我们使用一个数组 `blocks` 来表示木块的状态,初始状态下,每个木块的编号就是它在数组中的下标加一。 接着,我们通过一个 `print_blocks` 函数来打印木块的状态。该函数会依次输出数组中每个元素的值,然后换行。 在主函数中,我们首先使用一个循环来读入用户输入的移动指令。每次读入两个数字,分别表示要移动的木块的起点和终点。接着,我们使用两个循环来找到起点和终点在数组中的下标。然后,我们将起点上面的所有木块移回到原位,并将起点放到终点的上面。最后,我们打印移动后的状态。 值得注意的是,这段代码并没有对输入的数据做任何的错误处理,例如,如果用户输入的数字不在 1 到 10 的范围内,程序会出现错误。如果需要,可以在程序中添加相应的错误处理逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大风车滴呀滴溜溜地转

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值