题目链接:Problem B

题意:有n块木块,编号为0~n-1,要求模拟以下4种操作(下面的a和b都是木块编号)

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所在木块堆的顶部。

遇到quit时终止一组数据,忽略非法指令。

思路:每个木块堆都用一个vector来维护,move操作都要对a归位,onto操作都要对b归位可以一起处理,四种操作最后都是把a及以上的摞在b的顶上,只不过前两种a上面的为空。

note:
、resize(n)
调整容器的长度大小,使其能容纳n个元素。
如果n小于容器的当前的size,则删除多出来的元素。
否则,添加采用值初始化的元素。
、 resize(n, t)
多一个参数t,将所有新添加的元素初始化为t。

code:

 #include <iostream>
#include <string>
#include <vector>
using namespace std;
const int MAXN = ;
vector<int> pile[MAXN];
int n; void findBlock(int a, int& p, int& h)
{
for (p = ; p < n; ++p)
{
for (h = ; h < pile[p].size(); ++h)
{
if (pile[p][h] == a)
return;
}
}
} void clearAbove(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 + );
} void pileOnto(int pa, int ha, int pb)
{
for (int i = ha; i < pile[pa].size(); ++i)
pile[pb].push_back(pile[pa][i]);
pile[pa].resize(ha);
} int main()
{
cin >> n;
for (int i = ; i < n; ++i) pile[i].push_back(i);
int a, b;
string s1, s2;
while (cin >> s1)
{
if (s1 == "quit") break;
cin >> a >> s2 >> b;
int pa, pb, ha, hb;
findBlock(a, pa, ha);
findBlock(b, pb, hb);
if (pa == pb) continue;
if ("move" == s1) clearAbove(pa, ha);
if ("onto" == s2) clearAbove(pb, hb);
pileOnto(pa, ha, pb);
}
for (int i = ; i < n; ++i)
{
cout << i << ":";
for (int j = ; j < pile[i].size(); ++j)
cout << " " << pile[i][j];
cout << endl;
}
return ;
}

Problem B The Blocks Problem(vector的使用)的更多相关文章

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

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

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

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

  3. The Blocks Problem(vector)

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

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

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

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

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

  6. POJ 1208 The Blocks Problem

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

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

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

  8. Problem : 1002 ( A + B Problem II )

    经验总结:一定要注意输出的格式,字符的空格,空行,一定要观察清楚.如本题的最后一个输出结果后面没有空行.最后代码实现的时候需要判断一下,代码如下 !=n) cout<<endl; Prob ...

  9. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem B. Travelling Camera Problem set贪心

    Problem B. Travelling Camera Problem 题目连接: http://www.codeforces.com/gym/100253 Description Programm ...

随机推荐

  1. discuz函数quote

    public static function quote($str, $noarray = false) { if (is_string($str)) return '\'' . addcslashe ...

  2. 清风注解-Swift程序设计语言

    前言 Apple 发布了全新的 Swift 程序设计语言,用来开发 iOS 和 OS X 平台的应用程序.其目的不言而喻:就是为了给老迈的 Objective-C 一个合适接班人!因此,不难预见,未来 ...

  3. 新iPhone开售现场:土豪金火了

    今日苹果在全球范围内开始正式发售iPhone 5s与5c手机.在中国王府井苹果零售店,首位购机者李先生在今早4点50分便在此等候. 现场未发现黄牛 本次苹果在中国大陆的自有零售店渠道首批iPhone ...

  4. C#实现给手机发送短信

    public static void SendSMS(string FaSongUser, string ToUserList, string ContentStr)    {        //针对 ...

  5. Asp.Net写文本日志

    底层代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...

  6. c# 柱状图(转载)

    // c# 显示柱状图 using System; using System.Data; using System.Configuration; using System.Web; using Sys ...

  7. ASP.NET MVC Controller向View传值方式总结

    Controller向View传值方式总结 总结发现ASP.NET MVC中Controller向View传值的方式共有6种,分别是: ViewBag ViewData TempData 向普通Vie ...

  8. Farpoint Spread 常用事件

    1 // 单元格编辑结束,焦点离开或者按回车键时 比如:你要判断当前单元格输入内容是否合法.private void fpSpread1_EditModeOff(object sender, Syst ...

  9. 微信支付java版V3验证数据合法性

    [TOC] 1. 微信支付java版V3验证数据合法性 概要:使用微信支付接口时,微信会返回或回调给商户XML数据,开发者需要验证微信返回的数据是否合法. 特别提醒:商户系统对于支付结果通知的内容一定 ...

  10. malloc 申请得到的内存后,再 free 释放它的时候,操作系统会立即收回那块内存吗?

    stackoverflow上的回答: In many malloc/free implementations, free does normally not return the memory to  ...