The Blocks Problem(stl)

这篇博客介绍了一个简单的计算机科学问题,涉及到在一个由机器人臂操纵的块世界中执行命令。问题要求解析一系列指令,指导机器人臂如何处理平面上的块。四种合法命令包括移动、堆叠和归位块。程序需要处理这些命令直到遇到'quit'为止。博客给出了输入输出示例,并提供了一个使用C++实现的解决方案,通过vector来模拟块的移动状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

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 nn blocks on the table (numbered from 0 to n−1n−1) with block bibi adjacent to block bi+1bi+1 for all 0≤i<n−10≤i<n−1 as shown in the diagram below: 

The valid commands for the robot arm that manipulates blocks are:

  • move aa onto bb

where aa and bb are block numbers, puts block aa onto block bb after returning any blocks that are stacked on top of blocks aa and bb to their initial positions.

  • move aa over bb

where aa and bb are block numbers, puts block aa onto the top of the stack containing block bb, after returning any blocks that are stacked on top of block a to their initial positions.

  • pile aa onto bb

where aa and bb are block numbers, moves the pile of blocks consisting of block aa, and any blocks that are stacked above block aa, onto block bb. All blocks on top of block bb are moved to their initial positions prior to the pile taking place. The blocks stacked above block aa retain their order when moved.

  • pile aa over bb

where aa and bb are block numbers, puts the pile of blocks consisting of block aa, and any blocks that are stacked above block aa, onto the top of the stack containing block bb. The blocks stacked above block a retain their original order when moved.

  • quit

terminates manipulations in the block world.

Any command in which a=ba=b or in which aa and bb 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.

Input

The input begins with an integer nn on a line by itself representing the number of blocks in the block world. You may assume that 0<n<250<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 ii ( 0≤i<n0≤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., nn lines of output where nn is the integer on the first line of input).

Samples

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

Output

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

 分析:

读英文题太难受了,没别的办法只能多做题了。

题意:

一开始0~n-1号块上只有自己本身:1上是1,2上是2...

四个操作:

1.把a和b上面的块归位(1回到1号,2回到2号...),再把a放到b上

2.把a上面的归位,b不用,再把a接到b所在块上。

3.b上面的归位,把a及其上面的块放到b上

4.直接把a及其上面的块接到b所在块上

用vector模拟。

#include<bits/stdc++.h>
using namespace std;
vector<int> v[30];
int n;
int find1(int a)//返回在哪一列 
{
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<v[i].size();j++)
		{
			if(v[i][j]==a)return i;
		}
	}
}
int find2(int a)//返回在某一列的哪一个位置 
{
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<v[i].size();j++)
		{
			if(v[i][j]==a)return j;
		}
	}
}
void guiwei(int i,int j)//归位 
{
	int t;
	if(j>=v[i].size()-1)return;
	for(t=j+1;t<v[i].size();t++)
	{
		v[v[i][t]].push_back(v[i][t]);
	}
		v[i].erase(v[i].begin()+j+1,v[i].end());//归位后记得要删除 
	
}
int main()
{
	int i,j;
	cin>>n;
	string s1,s2;
	
	for( i=0;i<n;i++)v[i].push_back(i);
	while(cin>>s1)
	{
		int a,b;
		if(s1=="quit")break;
		cin>>a>>s2>>b;
		if(a==b)continue;//细节1: 同一个数不用管 
		int x1,x2,x3,x4;
		x1=find1(a);x2=find2(a);//查找a b的位置 
		x3=find1(b);x4=find2(b);
		if(x1==x3)continue;//细节2:在同一列不用管 
		if(s1=="move")
		{
			if(s2=="onto")
			{
				guiwei(x1,x2);
				guiwei(x3,x4);
				v[x1].erase(v[x1].begin()+x2);
				v[x3].push_back(a);
			}
			else
			{
				guiwei(x1,x2);
				v[x1].erase(v[x1].begin()+x2);
				v[x3].push_back(a);
			}
		}
		else if(s1=="pile")
		{
			if(s2=="onto")
			{
				guiwei(x3,x4);
				
				for(i=x2;i<v[x1].size();i++)
				v[x3].push_back(v[x1][i]);
			    
			    v[x1].erase(v[x1].begin()+x2,v[x1].end());
			    
			}
			else 
			{
				for(i=x2;i<v[x1].size();i++)
				{
				v[x3].push_back(v[x1][i]);
			    }
			    
			    v[x1].erase(v[x1].begin()+x2,v[x1].end());
			}
		}
	}
	for(i=0;i<n;i++)
	{
		cout<<i<<':';
		if(v[i].size()!=0)
		{
		for(j=0;j<v[i].size();j++)
		{
			cout<<' '<<v[i][j];
		}
	    }
		cout<<'\n';
	}
	
}

第一次发博客,没有大佬们的高超技艺,只想记录一下学习历程,多总结沉淀,希望和大家共进步!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值