The Blocks Problem(问题的抽象)

1.描述:

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 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相临
在这里插入图片描述
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.

还原a,b块上的块,把a放到b上

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.

还原a块上的块,把a放到含有b的那一堆上

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.

还原b块上的块,把a以及a上的部分,放到b上

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.

把a以及a上的部分放到含b的堆上

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.

当a = b或a和b处在同一摞时,任何企图操作a和b的命令都是非法的。所有非法的命令都要忽略,且不能对当前积木的状态产生作用。

2. 输入:

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.

3.输出:

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. 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).

4.样例输入:

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

5.样例输出:

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

6.题目大意:

有n个块,编号0 ~ n-1,给出四种操作

1.move a onto b
还原a,b块上的块,把a放到b上
2.move a over b
还原a块上的块,把a放到含有b的那一堆上
3.pile a onto b
还原b块上的块,把a以及a上的部分,放到b上
4.pile a over b
把a以及a上的部分放到含b的堆上

注意两个块位于同一堆时不进行操作

模拟对应操作

7.对标程的理解

自己写的很麻烦,所以看了看标程,标程的代码长度以及它的简便让我再次看到了编程之美,因为它确实很短(至少比我写的短很多),读了读标程,写一写我的理解:
标程很简洁,把四个操作中有共性的操作抽象成了函数,首先是还原操作,是把目标块上边的块还原,其次是移动操作,都可以总结成把a以及a上面的块放到b上(如果只有a一个块,那也说得过去),想要完成上面两个函数,需要两个很重要的参数,第一个是这两个块属于那一堆,另外一个就是目标块在堆中的高度,方便进行还原和移动;

这里边的resize()函数也很厉害,用设置大小来模拟删除;

8.标程

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
vector<int>ve[30];//模拟堆
string s1,s2;
int a,b,n;
int pa,pb;//第一个参数 目标堆的位置
int ha,hb;//第二个参数 在目标堆中的高度

void Printf()
{
	for(int i=0;i<n;i++)
	{
		printf("%d:",i);
		for(int j=0;j<ve[i].size();j++)
		{
			cout<<" "<<ve[i][j];
		}
		printf("\n");
	}
}//打印函数

void Search(int k,int &pk,int &hk)//这里很巧妙的用到了引用,可以把数带回
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<ve[i].size();j++)
		{
			if(ve[i][j]==k)
			{
				pk=i;
				hk=j;
				return ;
			}
		}
	}
}//搜索函数,找到目标堆的两个参数

void restore(int pk,int hk)
{
	for(int i=hk+1;i<ve[pk].size();i++)
	{
		int ss=ve[pk][i];
		ve[ss].push_back(ss);
	}
	ve[pk].resize(hk+1);//模拟删除,注意是hk+1,下标0开始
}//还原函数,把目标块上边的块还原


void move(int pk1,int pk2,int hka)//模拟移动
{
	for(int i=hka;i<ve[pk1].size();i++)
	{
		int ss=ve[pk1][i];
		ve[pk2].push_back(ss);
	}
	ve[pk1].resize(hka);//多余元素删除
}


int main()
{
	cin>>n;
	
	for(int i=0;i<n;i++)
	{
		ve[i].push_back(i);
	}
	
	while(cin>>s1)
	{
		if(s1=="quit")
		{
			Printf();
			return 0;
		}
		else
		{
			cin>>a>>s2>>b;
			Search(a,pa,ha);
			Search(b,pb,hb);
			if(pa==pb) continue;//同堆元素不处理
			
			if(s1=="move") restore(pa,ha);//两个move操作都要还原a
			if(s2=="onto") restore(pb,hb);//两个onto操作都要还原b
			//也是提取共性的一种体现
			move(pa,pb,ha);//移动
			
		}
	}
}

9.反思

我在一开始写的时候没注意到操作之间的共性,把每个操作都去模拟,最后的结果就是费时又费力,尤其是模拟删除的时候简直难写(我太菜了),而且很难debug,而把共性抽象出来后,写函数就变得简单又方便了

10.STL 函数库

STL模板函数

自己总结的模板函数和用法,持续更新

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

每天都想发疯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值