题目链接: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. STL中用erase()方法遍历删除元素

    STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque):另一类是以不连续的节点形式存储的容器(如:list.set.map).在使用erase方法来删除元素时 ...

  2. javascript编辑器预览模式解密

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. MySQL加强

    MySQL加强 Default Not null Unique Primary key Zerofill primary key auto_increment primary key auto_inc ...

  4. ZOJ 3829 Known Notation (2014牡丹江H称号)

    主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...

  5. 利用Socket实现的两个程序的通信

    写的也很简单,自己觉得挺有意思了 程序如图 主要代码 public class Message { Form1 mainfrom = null; public Message() { } public ...

  6. Oralce Exp 与 Imp 的使用方法

    1.完全:EXP  SYSTEM/SYSTEM@ORCL  FILE=C:\FULL.DMP  LOG=C:\FULL.DMP.LOG  FULL=Y  BUFFER=819200如果要执行完全导出, ...

  7. linq中的GroupBy总结

    1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述:Linq使用Group By按C ...

  8. BZOJ 1901: Zju2112 Dynamic Rankings( 树状数组套主席树 )

    裸的带修改主席树.. 之前用BIT套Splay( http://www.cnblogs.com/JSZX11556/p/4625552.html )A过..但是还是线段树好写...而且快(常数比平衡树 ...

  9. 用SCMD2.0.8.0汉化版制作OB地图简易教程

    [综合] [复制链接]     Fenix_king       153 主题 0 好友 1万 积分 金星 该用户从未签到 星币 6392 水晶 0 星望 22 精华 0 发消息 电梯直达 楼主   ...

  10. android小知识之意图(intent)

    android中的意图有显示意图和隐式意图两种, 显示意图要求必须知道被激活组件的包和class 隐式意图只需要知道跳转activity的动作和数据,就可以激活对应的组件 A 主activity  B ...