UVa 101 The Blocks Problem(vector)

个人博客vecor是一个不定长的数组。“封装”了很多常用的操作在vector内。比如,a.size()读取大小,a.resize()改变大小,a.push_back()向尾部添加元素,a.pop_back()删除最后一个元素,a.clear()清空,a.empty()测试是否为空。

vector是一个模板类,需要用vector<int>a或者vector<double>b这样的声明方式来声明一个vector。vector<int>是一个类似于int a[]的整型数组,vector<string>则是一个类似string a[]的字符串数组。

vector详细指令:http://hongxuelin.com/code/acm/zisu/83.html

个人博客题目:

  1. 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上面所有的积木都放回原处,再将a放在b上。
  2. 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和b都是积木的编号,先将a上面所有的积木放回原处,再将a放在b上。(b上原有积木不动)
  3. 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.
    • a和b都是积木的编号,将a和其上面所有的积极组成的一摞整体移动到b上。在移动前要先将b上面所有的积极都放回原处。移动的一摞积木要保持原来的顺序不变。
  4. 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和b都是积木的编号,将a和其上面所有的积极组成的一摞整体移动到b所在一摞积木的最上面一个积木上。移动的一摞积木要保持原来的顺序不变。
  5. quit
    • terminates manipulations in the block world.
    • 结束积木世界的操纵。

个人博客代码并不能通过oj只是用来学习STL:vector。

文章转载自洪学林个人博客:http://www.hongxuelin.com/

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
          
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 30;
int n;
vector<int>pile[maxn];
void find_block(int a,int &p,int &h)
{
for(p=0;p<n;p++)
{
for(h=0;h<pile[p].size();h++)
{
if(a==pile[p][h]) 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);//归回
}
pile[p].resize(h+1);//重新设定大小 ,保留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);
}
int main()
{
int a,b;
while(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);
}
//output
for(int i=0;i<n;i++)
{
printf("%d:",i);
for(int j=0;j<pile[i].size();j++) printf(" %d",pile[i][j]);
cout<<endl;
}
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值