题目:

输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为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所在的木块的堆的上面。

当输入quit时,结束操作并输出0~n-1的位置上的木块情况

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.提取指令之间的共同点,编写函数以减少代码量
经观察:
pile a onto b和move a onto b,在移动之前,都需要把b上方木块归位。
move a onto b和move a over b,在移动之前,都需要把a上方木块归位。
两个都是一个意思,把第p堆高度位h的木块上方的所有木块移回原位
四条指令在做完预备工作(归位)之后,都是把a这个堆高度位h及上方的木块整体移到b这个堆整体的顶部

有人要问为什么需要有h
如果
2:2 3 4 5
6:6 7 8 9

pile 3 over 6

2:2
6:6 7 8 9 3 4 5
懂了吧,并不是只有从最底部开始移动
2.利用vector
vector< int>pile[maxn]像是一个二维数组,只不过一维大小固定二位不固定
vector< int>a
a.size()读取大小
a.resize(b)改变大小(只保留前b个数,下标0到b-1的元素)
a.push_back(b)向尾部添加元素b
a.pop_back()删除最后一个元素
本题每个木块堆的高度不确定,而木块堆的个数确定有n个,因此用vector合适

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
const int maxn = 30;
int n;
vector<int> pile[maxn];//找到木块a所在的pile和height,以引用的形式返回调用者void find_block(int a,int&p,int &h){//p和h是需要通过这个函数求的,由于是引用,所以return后p,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 m=pile[p][i];pile[m].push_back(m);}pile[p].resize(h+1);
}
//把第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 (cin >> s1 ,s1!="quit"){   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;
}

(STL,vector)木块问题相关推荐

  1. C++ stl vector介绍

    转自: STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if ...

  2. STL vector的几种清空容器(删除)办法

    1.为什么需要主动释放vector内存 来自 <https://blog.csdn.net/hellokandy/article/details/78500067> vector其中一个特 ...

  3. STL vector 容器介绍

    介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  4. STL vector的erase操作问题

    STL vector的erase操作问题 一老大说CSDN上有篇博文("关于STL vector的erase操作",地址是:http://blog.csdn.net/tingya/ ...

  5. STL vector容器

    介绍  这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用. ...

  6. stl vector 函数_在C ++ STL中使用vector :: begin()和vector :: end()函数打印矢量的所有元素...

    stl vector 函数 打印向量的所有元素 (Printing all elements of a vector) To print all elements of a vector, we ca ...

  7. stl vector 函数_vector :: at()函数以及C ++ STL中的示例

    stl vector 函数 C ++ vector :: at()函数 (C++ vector::at() function) vector::at() is a library function o ...

  8. stl vector 函数_vector :: crend()函数以及C ++ STL中的示例

    stl vector 函数 C ++ vector :: crend()函数 (C++ vector::crend() function) vector::crend() is a library f ...

  9. stl vector 函数_vector :: pop_back()函数以及C ++ STL中的示例

    stl vector 函数 C ++ vector :: pop_back()函数 (C++ vector::pop_back() function) vector::pop_back() is a ...

  10. stl vector 函数_vector :: push_back()函数,以及C ++ STL中的示例

    stl vector 函数 C ++ vector :: push_back()函数 (C++ vector::push_back() function) vector::push_back() is ...

最新文章

  1. 力扣(LeetCode)刷题,简单题(第9期)
  2. 独家 | 一文读懂最大似然估计(附R代码)
  3. 迷失在小镇上的日记(16)
  4. VTK修炼之道21:图像基本操作_彩色图像生成灰度图像
  5. 网易技术干货 | 云信移动端音视频UI自动化测试实践
  6. 从中序与后序遍历序列构造二叉树Python解法
  7. 【bzoj1263】[SCOI2006]整数划分 高精度
  8. rust模组服如何切换标准服_资讯DNF手游将于3月20日发放10000个体验服资格(附申请方法)...
  9. java求正整数和_求连续正整数的和-Java
  10. 大数据Spark面试题
  11. php木马检测关键词
  12. JS中的debugger调试(谷歌浏览器)
  13. 研究Google maps及51ditu的图片切割及存储方法(转)
  14. LeetCode#860: 柠檬水找零
  15. win7 计算机无法搜索文件夹,win7文件搜索功能不能使用了
  16. php微信群,PHP微信群加群强制分享转发裂变源码
  17. 宝马将自动驾驶和打车服务结合,最快明年在德国上路
  18. linux 下的程序格式,linux 下软件安装包的格式有哪些?
  19. Linux触摸屏动效
  20. gradle-6.7-all 快速下载

热门文章

  1. struts2的java.lang.NoSuchMethodException异常处理
  2. ssh框架常见错误与解决方法
  3. MXNet结合kubeflow进行分布式训练
  4. python导入mysqldb_Python导入MySQLdb报错
  5. cef在android中使用_关于富文本在Android中的应用以及遇到的坑
  6. python列表生成式内必须定义匿名函数_Python基础-----基础概念总结
  7. 服务器能像客户端发信息吗,服务器怎么向客户端发信息吗
  8. mysql 5.7 flashback_Flashback for MySQL 5.7
  9. threadlocal存连接对象的目的_终于懂了ThreadLocal,不再害怕面试官问了
  10. Win11系统点击回滚没有反应是怎么回事