[UVA101]The Blocks Problem 题解(不定长数组:vector详解)

The Blocks Problem - 洛谷 

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: 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 a retain 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.

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.

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. 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,要求实现下列四种操作:

  • move a onto b : 把a和b上方的木块归位,然后把a放到b上面。
  • move a over b : 把a上方的木块归位,然后把a放在b所在木块堆的最上方。
  • pile a onto b : 把b上方的木块归位,然后把a及以上的木块整体放到b上面。
  • pile a over b : 把a及以上的木块整体放到b所在木块堆的顶端。
  • 一组数据的结束标志为 quit,如果有非法指令(如a与b在同一堆),无需处理。

输出:所有操作输入完毕后,从左到右,从下到上输出每个位置的木块编号。

题解

vector 说明

  • vector是向量类型,可以容纳许多类型的数据,因此也被称为容器
  • (可以理解为动态数组,是封装好了的类)
  • 进行vector操作前应添加头文件#include <vector>

vector初始化


方式1.定义具有10个整型元素的向量(尖括号为元素类型名,它可以是任何合法的数据类型),不具有初值,其值不确定
        vector<int>a(10);
方式2.定义具有10个整型元素的向量,且给出的每个元素初值为1
        vector<int>a(10,1);
方式3.用向量b给向量a赋值,a的值完全等价于b的值
        vector<int>a(b);
方式4.将向量b中从0-2(共三个)的元素赋值给a,a的类型为int型
        vector<int>a(b.begin(),b.begin+3);
方式5.从数组中获得初值
        int b[7]={1,2,3,4,5,6,7};
        vector<int> a(b,b+7);

vector对象的常用内置函数使用

#include<vector>
vector<int> a,b;
//b为向量,将b的0-2个元素赋值给向量a
a.assign(b.begin(),b.begin()+3);
//a含有4个值为2的元素
a.assign(4,2);
//返回a的最后一个元素
a.back();
//返回a的第一个元素
a.front();
//返回a的第i元素,当且仅当a存在
a[i];
//清空a中的元素
a.clear();
//判断a是否为空,空则返回true,非空则返回false
a.empty();
//删除a向量的最后一个元素
a.pop_back();
//删除a中第一个(从第0个算起)到第二个元素,也就是说删除的元素从a.begin()+1算起(包括它)一直到a.begin()+3(不包括它)结束
a.erase(a.begin()+1,a.begin()+3);
//在a的最后一个向量后插入一个元素,其值为5
a.push_back(5);
//在a的第一个元素(从第0个算起)位置插入数值5,
a.insert(a.begin()+1,5);
//在a的第一个元素(从第0个算起)位置插入3个数,其值都为5
a.insert(a.begin()+1,3,5);
//b为数组,在a的第一个元素(从第0个元素算起)的位置插入b的第三个元素到第5个元素(不包括b+6)
a.insert(a.begin()+1,b+3,b+6);
//返回a中元素的个数
a.size();
//返回a在内存中总共可以容纳的元素个数
a.capacity();
//将a的现有元素个数调整至10个,多则删,少则补,其值随机
a.resize(10);
//将a的现有元素个数调整至10个,多则删,少则补,其值为2
a.resize(10,2);
//将a的容量扩充至100,
a.reserve(100);
//b为向量,将a中的元素和b中的元素整体交换
a.swap(b);
//b为向量,向量的比较操作还有 != >= > <= <
a==b;

vector就是一个不定长数组。不仅如此,它把一些常用操作封装在了vector类型内部。例如若a是一个vector,可以用a.size()读取它的大小a.resize改变大小a.push_back()向尾部添加元素a.pop_back()删除最后一个元素
vector是一个模板类,所以需要用vector<int> a或者 vector<double>b这样的方式来声明一个vector。vector<int>是一个类似于int a[]的整数数组,而vector<string>就是一个类似string a[]的字符串数组。他们可以直接赋值,还可以作为函数的参数或者返回值,而无需像传递数组那样另外用一个变量指定元素个数。

标准答案

#include<cstdio>
#include<string>
#include<vector>			//每个木块堆的高度不确定,所以用vector来保存更合适 
#include<iostream>
using namespace std;

const int N=35;
int n;
vector<int> pile[N]; 		//每个pile是一个vector

//找出木块a所在的pile和hight,以引用的形式返回调用者 
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;
			}
		} 
	}
} 

//把第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的元素 
} 

//把第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);
} 

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(1){
		cin>>s1;
		if(s1=="quit"){
			break;
		} 
		cin>>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;
} 

数据结构的核心是vector<int> pile[maxn],所有操作都是围绕它进行的。vector就像一个二维数组,只是第一维的大小是固定的(不超过maxn),但第二维的大小不固定。上述代码还有一个值得学习的技巧:输入一共有4种指令,但如果完全独立地处理各指令,代码就会变得冗长而且易错。更好的方法是提取出指令之间的共同点,编写函数以减少重复代码。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

古谷彻

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

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

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

打赏作者

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

抵扣说明:

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

余额充值