题意:

有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. PHP MYSQL 数据库配置连接

    //配置文件 Config.php // 默认使用数据库配置 ***************************************** $Config['ConnectTag'] = 'de ...

  2. javascript event兼容IE和FF

    事件对象在IE和FF下的兼容写法 function abc(event){ var e=event||window.event; //键盘码的捕获 var key=e.which||e.keyCode ...

  3. hdu 4622 Reincarnation SAM模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有Q次区间查询(Q <= 10000),问区 ...

  4. EXTJS 4.2 资料 控件之Window窗体相关属性的用法

    最大化,最小化,是否显示关闭按钮 var win_CommonPicLibMultiple = Ext.create("Ext.window.Window", { title: & ...

  5. Contest2037 - CSU Monthly 2013 Oct(中南大学2013年10月月赛水题部分题解)

    Problem A: Small change 题解:http://www.cnblogs.com/crazyapple/p/3349469.html Problem B: Scoop water 题 ...

  6. [原创] zabbix学习之旅一:源码安装

    zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位/解决存 ...

  7. GPS导航仪常见术语解释

    摘自百度百科: 坐标(coordinate) 有2维.3维两种坐标表示,当GPS能够收到4颗及以上卫星的信号时,它能计算出本地的3维坐标:经度.纬度.高度,若只能收到3颗卫星的信号,它只能计算出2维坐 ...

  8. C++函数中那些不可以被声明为虚函数的函数

    转自C++函数中那些不可以被声明为虚函数的函数 常见的不不能声明为虚函数的有:普通函数(非成员函数):静态成员函数:内联成员函数:构造函数:友元函数. 1.为什么C++不支持普通函数为虚函数? 普通函 ...

  9. Unity3D 自动打包整个项目(以AssetBundle实现)

    原地址:http://blog.csdn.net/huang7jiao/article/details/18370653 需求: 在移动开发中,手动控制资源的加载.释放和热更新,是很有必要的. 而Un ...

  10. spring中注解事务认识

    1.配置事务管理器 <!-- 设定transactionManager事务管理器 --> <bean id="txManager" class="org ...