题意:

有n个木块及n个木块堆,初始状态是第i个木块在第i个木块堆上。对应有四种操作,然后输出最终状态。

分析:

用一个vector<int>模拟一个木块堆,进行相应操作即可。

 #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
using namespace std; const int maxn = ;
int n;
vector<int> pile[maxn]; void find_block(int a, int& p, int& h)
{
for(int i = ; i < n; ++i)
for(int j = ; j < pile[i].size(); ++j)
if(pile[i][j] == a)
{ p = i; h = j; }
} void clear_block(int p, int h)
{//将第p堆高度为h的上方的木块归位
for(int i = h+; i < pile[p].size(); ++i)
{
int k = pile[p][i];
pile[k].push_back(k);
}
pile[p].resize(h+);
} void move_onto(int pa, int h, int pb)
{//将pa堆高度为h及以上的木块摞到pb堆上
for(int i = h; i < pile[pa].size(); ++i)
pile[pb].push_back(pile[pa][i]);
pile[pa].resize(h);
} int main()
{
//freopen("in.txt", "r", stdin);
int a, b;
scanf("%d", &n);
for(int i = ; i < n; ++i) pile[i].push_back(i);
string s1, s2;
while(cin >> s1 >> 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_block(pb, hb);
if(s1 == "move") clear_block(pa, ha);
move_onto(pa, ha, pb);
}
for(int i = ; i < n; ++i)
{
printf("%d:", i);
for(int j = ; j < pile[i].size(); ++j) printf(" %d", pile[i][j]);
printf("\n");
} return ;
}

代码君

UVa 101 (模拟) The Blocks Problem的更多相关文章

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

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

  2. 【例题5-2 UVA - 101】The Blocks Problem

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用vector模拟就好. resize的时候,只是把多余的清理掉. 原先的不会变的. [错的次数] 在这里输入错的次数 [反思] 在 ...

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

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

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

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

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

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

  6. Uva 101 -- the block problem

    Uva 101 the block problem 题目大意: 输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为0~n-1的位置.现对这些木块进行操作,操作分为四种. 1.move a o ...

  7. POJ 1208 The Blocks Problem

    The Blocks Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5397   Accepted: 231 ...

  8. The Blocks Problem(vector)

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

  9. UVA 10245 The Closest Pair Problem 最近点问题 分治算法

    题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...

随机推荐

  1. 拓展,Fibonacci螺旋

    #该程序由023递归这课中的fibonacci数列递归写法修改而成 #在写的过程中发现,如果要正确引导用户的每一次输入,写的代码比主程序还要多 #当然,为了使程序在用户交互过程中显得更加友好,提供错误 ...

  2. WP-PostViews Plus停止计数

    最近老是发现WP-PostViews Plus停止计数,网上查了资料才发现,WP Super Cache和WP-PostViews Plus存在冲突.根据自己网站目前的情况看,暂时用不到WP Supe ...

  3. springmvc整合redis架构搭建实例

    新换环境,又有新东西可以学习了,哈皮! 抽空学习之余看了一下redis,个人对Springmvc的爱是忠贞不渝,所以整理了一下Springmvc整合redis的环境搭建.分享学习. 第一步: 创建ma ...

  4. parsing html in asp.net

    http://stackoverflow.com/questions/5307374/parsing-html-page-in-asp-net http://htmlagilitypack.codep ...

  5. hive-site.xml 参数设置

    <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="confi ...

  6. linux驱动系列之调试环境搭建一

    2014年刚开始学习linux时,搭建环境花了很多时间.当时最熟悉的是单片机如Mag16和stm32,依据以往学习单片机的经验肯定要用下载器下载程序,但是我找了很久没有比较好的IDE,不像Mag16有 ...

  7. Android支付接入(一):支付宝

    原地址:http://blog.csdn.net/simdanfeg/article/details/9011603 转载之前我想深深地感谢屌丝哥 相信相同过App获取利润的都会需要接入计费SDK,下 ...

  8. Chp2: Linked List

    2.2 Implement an algorithm to find the kth to last element of a singly linked list. Just using " ...

  9. AppStore 沙箱环境的测试流程

    1:请确保你打得版本是 沙箱环境的版本 2:请确保的手机序列号已经加入沙箱环境3:请确保你的手机Apple ID 账户已经退出 ------ 如果这些都准备好了,再进行测试吧,不然一堆的未知问题等着你 ...

  10. xcode 树形管理 cocos2d-x的资源

    把资源以目录的形式加入xcode中, 同时, 在加入时, 选择"Create Folder References for any  added folders", 而不是默认的&q ...