uvaoj 101 - The Blocks Problem(vector应用+技巧)
木块问题,模拟堆的操作。每个堆的高度不确定,用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所在木块堆的顶部。
这里有一个技巧:四种指令完全独立处理会使代码变得复杂,容易出错。所以可以提取出他们之间的共同点,以减少重复代码。
- #include<bits/stdc++.h>
- using namespace std;
- const int maxn=;
- int n;
- vector<int> pile[maxn];//每个pile[i]是一个vector
- //找木块a所在的pile和height,以引用的形式返回调用者
- void find_block(int a,int& p,int& h)
- {
- for(p=;p<n;p++)
- {
- for(h=;h<pile[p].size();h++)
- {
- if(pile[p][h]==a)return;
- }
- }
- }
- //第P堆高度为h的木块上方的所有木块移回原位
- void clear_above(int p,int h)
- {
- for(int i=h+;i<pile[p].size();i++)
- {
- int b=pile[p][i];
- pile[b].push_back(b);
- }
- pile[p].resize(h+);//第p堆只保留下标在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);//第p堆只保留下标在0~h-1之间的元素
- }
- //打印函数
- void print()
- {
- for(int i=;i<n;i++)
- {
- printf("%d:",i);
- for(int j=;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=;i<n;i++)pile[i].push_back(i);//初始化,给木块赋上相应对的编号
- while(cin>>s1)
- {
- if(s1!="quit")cin>>a>>s2>>b;
- else break;
- 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 ;
- }
uvaoj 101 - The Blocks Problem(vector应用+技巧)的更多相关文章
- UVa 101 The Blocks Problem Vector基本操作
UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...
- UVa 101 - The Blocks Problem(积木问题,指令操作)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- POJ 1208 The Blocks Problem --vector
http://poj.org/problem?id=1208 晚点仔细看 https://blog.csdn.net/yxz8102/article/details/53098575 #include ...
- UVa 101 The Blocks Problem
题意:给出从左到右放置的n块木块(从0开始编号),再给出四种操作,再给出相应的操作,输出操作结束后每一堆木块的情况. 学习的紫书,因为每一堆的木块数是在发生变化的,所以用vector. 然后就是模拟几 ...
- UVa 101 - The Blocks Problem STL
题目:给你n个方块,有四种操作: .move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面: .move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上 ...
- 【UVA - 101】The Blocks Problem(vector+模拟)
The Blocks Problem Descriptions:(英语就不说了,直接上翻译吧) 初始时从左到右有n个木块,编号为0~n-1,要求实现下列四种操作: move a onto b: 把a和 ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
- The Blocks Problem(vector)
题目链接:http://poj.org/problem?id=1208 The Blocks Problem Time Limit: 1000MS Memory Limit: 10000K Tot ...
- UVa101 The Blocks Problem(不定长数组vector)
The Blocks Problem 书上的一道例题,代码思路比较清晰,可以看懂. 相关知识: 若a是一个vector,则: a.size():读取它的大小 a.resize():改变大小 a.pus ...
随机推荐
- 面试准备——(三)Selenium面试题总结
一.Selenium基本知识 1. 什么是Selenium? Selenium是浏览器自动化工具,主要用来Web的自动化测试,以及基于Web的任务管理自动化.它支持的语言有:python.Java.r ...
- 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 ...
- sql修改数据库中表的结构
ALTER TABLE TableName1 ADD | ALTER [COLUMN] FieldName1 FieldType [(nFieldWidth [, nPrecision])] [NUL ...
- jquery固定位置浮动
示例: <!DOCTYPE html> <html> <head> <title>test page</title> <script ...
- BZOJ2286: [Sdoi2011]消耗战(虚树/树形DP)
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 5246 Solved: 1978[Submit][Status][Discuss] Descript ...
- python3爬取全站美眉图片
爬取网站:https://www.169tp.com/xingganmeinv 该网站美眉图片有数百页,每页24张,共上万张图片,全部爬取下来 import urllib.request import ...
- ;(function($,window,document,undefined){})(jQuery,window,document)
;(function($,window,document,undefined){})(jQuery,window,doucment) 1.自调函数(function(){})() 2.好处是不会产生任 ...
- JS 创建对象总结
狭义:new 构造函数. (注:在JS中创建对象只有一种方式,就是new 构造函数.其中字面量的方式是一种语法糖,本质仍然是new 构造函数) 广义:工厂模式(解决复杂度) 构造函数模式(解决复杂度, ...
- Windows常用命令,想要看什么命令直接在全文“CTRL+F”检索(转)
原文地址:https://www.cnblogs.com/kekec/p/3662125.html 打开"运行"对话框(Win+R),输入cmd,打开控制台命令窗口... 也可以通 ...
- 【C】switch-case里面,加或不加break的区别
int test; test = ; switch(test) { : test++; printf("value = 0"); // 打印printf,后续没有break代码,系 ...