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,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...
随机推荐
- c++MMMMM:oo
1.union,struct和class的区别
- 自动化测试报告之allure使用基础指南
差不多三个月前些的教程,然后跳槽了,自定义模块还没有写....后续也不知道有时间补上没有,最近应该会毕竟专注app测试这块了 1.github下载allure安装包:https://githu ...
- yum源遇到的问题
1.在配置CentOS的本地yum源时,所遇到的问题,本地yum设置失败 步骤: vim /etc/yum.repos.d/local.repo 设置本地源 可能会遇到本地源问题,注意使用tab键 ...
- python 教程(一)
必须感慨一下,整了一周多的时间才基本理通顺.自己老是不能静心,方法也不对所以走了很多弯路. 1.建议:一定要先去看官方文档. 下面我们来看一周的成果吧: windows下如何下载并安装Python 3 ...
- ubuntu16.04安装cuDNN
cudnn的安装非常简单 (1)下载安装文件 按需求下载cudnn的安装文件:https://developer.nvidia.com/rdp/cudnn-archive Tar File的下载如下图 ...
- 更新protobuf
sudo pip install --upgrade protobuf sudo apt-get install libprotobuf-dev protobuf-compiler这个不知道是什么问题 ...
- 处理并解决mysql8.0 caching-sha2-password问题,开启远程访问
原文:https://blog.csdn.net/u010026255/article/details/80062153 启动mysql服务:service mysqld start ALTER US ...
- 线程异步更新UI
winform程序一般是不允许非主线程操作ui,单可以通过线程与委托的方式并结合Control类提供的BeginInvoke机制进行ui更改 如下,这是更新ui的方法 private void upU ...
- 【leetcode】1257. Smallest Common Region
题目如下: You are given some lists of regions where the first region of each list includes all other reg ...
- mvc api 关于 post 跟get 请求的一些想法[FromUri] 跟[FromBody] 同一个控制器如何实现共存
wep api 在设置接收请求参数的时候,会自动根据模型进行解析. [FromUrl] 跟[FromBody] 不可以同时使用. 要拆分开: [HttpGet] public object GetP ...