Uva 101 -- the block problem
Uva 101 the block problem
题目大意:
输入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:
学习使用vector
vector就是一个不定长数组。
size()大小
resize()改变大小
push_back()向尾部添加元素
pop_back()删除最后一个元素
empty()测试是否为空
clear()清空
- #include<iostream>
- #include<cstdio>
- #include<string>
- #include<vector>
- using namespace std;
- const int maxn = ;
- int n;
- vector<int> pile[maxn];
- ///找木块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);//把木块b放回原位
- }
- pile[p].resize(h+);//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 = ;i<n;i++)
- {
- cout<<i<<":";
- for(int j = ;j<pile[i].size();j++)
- cout<<" "<<pile[i][j];
- cout<<endl;
- }
- }
- int main()
- {
- int a,b;
- cin>>n;
- string s1,s2;
- for(int i = ;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 ;
- }
Uva 101 -- the block problem的更多相关文章
- 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 ...
- 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所在的方块堆的上 ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
- 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem
UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- uva 10837 - A Research Problem(欧拉功能+暴力)
题目链接:uva 10837 - A Research Problem 题目大意:给定一个phin.要求一个最小的n.欧拉函数n等于phin 解题思路:欧拉函数性质有,p为素数的话有phip=p−1; ...
- UVA 810 - A Dicey Problem(BFS)
UVA 810 - A Dicey Problem 题目链接 题意:一个骰子,给你顶面和前面.在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...
随机推荐
- 06 Python网络爬虫requets模块高级用法
一. 基于requests模块的cookie操作 - cookie概念: 当用户通过浏览器访问一个域名的时候,访问的web服务器会给客户端发送数据,以保持web服务器与客户端之间的状态保持,这些数据就 ...
- docker 入门(1)
1,docker 的安装卸载 https://docs.docker.com/install/linux/docker-ce/ubuntu/ 2,docker中的基本概念 镜像(Image) 容器(C ...
- postgres日常操作
1.启动pgsl数据库 [postgres@master ~]$ pg_ctl start [postgres@master data]$ pg_ctl -D /usr/local/pgsql/dat ...
- Linux系统下C语言获取Time
获取时间的函数有很多,具体包括如下: time()/gettimeofday()等等,下面是获取具体到usecond的时间程序: #include <iostream> #include ...
- deep_learning_Function_tensorflow_unpack()
tf.unpack(A, axis)是一个解包函数.A是一个需要被解包的对象,axis是一个解包方式的定义,默认是零,如果是零,返回的结果就是按行解包.如果是1,就是按列解包. 例如: from te ...
- 了解并安装Nginx
公司使用nginx作为请求分发服务器,发现本人在查看nginx配置上存在些许困难,故仔细阅读了陶辉的<深入理解nginx模块开发与框架>第一部分,并作此记录. 了解 我根据书上的思路来了解 ...
- 你在和脚本谈恋爱(自动化在IM聊天中的应用)
谢谢打开这篇文章的每个你 测开之分层自动化(Python)招生简章 Python自动化测试报告美化 在python中进行数据驱动测试 太嚣张了!他竟用Python绕过了“验证码” 在网络世界里你不知道 ...
- C#和Java的最大不同
本文摘抄自知乎. 作者:匿名用户链接:https://www.zhihu.com/question/20451584/answer/27163009来源:知乎著作权归作者所有.商业转载请联系作者获得授 ...
- Restricting Input in HTML Textboxes to Numeric Values
Ok, here’s a fairly basic one – how to force a textbox to accept only numeric input. Somebody asked ...
- mongodb aggregate
$project:修改输入文档的结构.可以用来重命名.增加或删除域,也可以用于创建计算结果以及嵌套文档. $match:用于过滤数据,只输出符合条件的文档.$match使用MongoDB的标准查询操作 ...