https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page=show_problem&problem=37

木块问题,模拟堆的操作。每个堆的高度不确定,用vector来做很合适(vector动态)。

本题模拟四个操作:

1.move a onto b:把a和b上方的木块全部放回原来的堆,然后把a摞在b上面

2.move a over b:把a上方的木块全部放回原来的堆,然后把a放在b所在木块堆的顶部

3.pile a onto b:把b上方的木块全部放回原来的堆,然后把a及上面的木块整体摞在b上面。

4.pile a over b:把a及上面的木块整体摞在b所在木块堆的顶部。

这里有一个技巧:四种指令完全独立处理会使代码变得复杂,容易出错。所以可以提取出他们之间的共同点,以减少重复代码。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=;
  4. int n;
  5. vector<int> pile[maxn];//每个pile[i]是一个vector
  6. //找木块a所在的pile和height,以引用的形式返回调用者
  7. void find_block(int a,int& p,int& h)
  8. {
  9. for(p=;p<n;p++)
  10. {
  11. for(h=;h<pile[p].size();h++)
  12. {
  13. if(pile[p][h]==a)return;
  14. }
  15. }
  16. }
  17. //第P堆高度为h的木块上方的所有木块移回原位
  18. void clear_above(int p,int h)
  19. {
  20. for(int i=h+;i<pile[p].size();i++)
  21. {
  22. int b=pile[p][i];
  23. pile[b].push_back(b);
  24. }
  25. pile[p].resize(h+);//第p堆只保留下标在0~h之间的元素
  26. }
  27. //把第p堆高度为h及其上方的木块整体移动到p2堆
  28. void pile_onto(int p,int h,int p2)
  29. {
  30. for(int i=h;i<pile[p].size();i++)
  31. {
  32. pile[p2].push_back(pile[p][i]);
  33. }
  34. pile[p].resize(h);//第p堆只保留下标在0~h-1之间的元素
  35. }
  36. //打印函数
  37. void print()
  38. {
  39. for(int i=;i<n;i++)
  40. {
  41. printf("%d:",i);
  42. for(int j=;j<pile[i].size();j++)printf(" %d",pile[i][j]);
  43. printf("\n");
  44. }
  45. }
  46. int main()
  47. {
  48. int a,b;
  49. cin>>n;
  50. string s1,s2;
  51. for(int i=;i<n;i++)pile[i].push_back(i);//初始化,给木块赋上相应对的编号
  52. while(cin>>s1)
  53. {
  54. if(s1!="quit")cin>>a>>s2>>b;
  55. else break;
  56. int pa,pb,ha,hb;
  57. find_block(a,pa,ha);
  58. find_block(b,pb,hb);
  59. if(pa==pb)continue;
  60. if(s2=="onto")clear_above(pb,hb);
  61. if(s1=="move")clear_above(pa,ha);
  62. pile_onto(pa,ha,pb);
  63. }
  64. print();
  65. return ;
  66. }

uvaoj 101 - The Blocks Problem(vector应用+技巧)的更多相关文章

  1. UVa 101 The Blocks Problem Vector基本操作

    UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...

  2. UVa 101 - The Blocks Problem(积木问题,指令操作)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  3. POJ 1208 The Blocks Problem --vector

    http://poj.org/problem?id=1208 晚点仔细看 https://blog.csdn.net/yxz8102/article/details/53098575 #include ...

  4. UVa 101 The Blocks Problem

    题意:给出从左到右放置的n块木块(从0开始编号),再给出四种操作,再给出相应的操作,输出操作结束后每一堆木块的情况. 学习的紫书,因为每一堆的木块数是在发生变化的,所以用vector. 然后就是模拟几 ...

  5. UVa 101 - The Blocks Problem STL

    题目:给你n个方块,有四种操作: .move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面: .move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上 ...

  6. 【UVA - 101】The Blocks Problem(vector+模拟)

    The Blocks Problem Descriptions:(英语就不说了,直接上翻译吧) 初始时从左到右有n个木块,编号为0~n-1,要求实现下列四种操作: move a onto b: 把a和 ...

  7. 木块问题(The Blocks Problem,Uva 101)

    不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...

  8. The Blocks Problem(vector)

    题目链接:http://poj.org/problem?id=1208 The Blocks Problem Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  9. UVa101 The Blocks Problem(不定长数组vector)

    The Blocks Problem 书上的一道例题,代码思路比较清晰,可以看懂. 相关知识: 若a是一个vector,则: a.size():读取它的大小 a.resize():改变大小 a.pus ...

随机推荐

  1. 面试准备——(三)Selenium面试题总结

    一.Selenium基本知识 1. 什么是Selenium? Selenium是浏览器自动化工具,主要用来Web的自动化测试,以及基于Web的任务管理自动化.它支持的语言有:python.Java.r ...

  2. HDU 1029 Ignatius and the Princess IV (map的使用)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/10 ...

  3. sql修改数据库中表的结构

    ALTER TABLE TableName1 ADD | ALTER [COLUMN] FieldName1 FieldType [(nFieldWidth [, nPrecision])] [NUL ...

  4. jquery固定位置浮动

    示例: <!DOCTYPE html> <html> <head> <title>test page</title> <script ...

  5. BZOJ2286: [Sdoi2011]消耗战(虚树/树形DP)

    Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 5246  Solved: 1978[Submit][Status][Discuss] Descript ...

  6. python3爬取全站美眉图片

    爬取网站:https://www.169tp.com/xingganmeinv 该网站美眉图片有数百页,每页24张,共上万张图片,全部爬取下来 import urllib.request import ...

  7. ;(function($,window,document,undefined){})(jQuery,window,document)

    ;(function($,window,document,undefined){})(jQuery,window,doucment) 1.自调函数(function(){})() 2.好处是不会产生任 ...

  8. JS 创建对象总结

    狭义:new 构造函数. (注:在JS中创建对象只有一种方式,就是new 构造函数.其中字面量的方式是一种语法糖,本质仍然是new 构造函数) 广义:工厂模式(解决复杂度) 构造函数模式(解决复杂度, ...

  9. Windows常用命令,想要看什么命令直接在全文“CTRL+F”检索(转)

    原文地址:https://www.cnblogs.com/kekec/p/3662125.html 打开"运行"对话框(Win+R),输入cmd,打开控制台命令窗口... 也可以通 ...

  10. 【C】switch-case里面,加或不加break的区别

    int test; test = ; switch(test) { : test++; printf("value = 0"); // 打印printf,后续没有break代码,系 ...