题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=37

将操作分解为基本步骤,编写函数,重复调用

C++11代码如下:

 #include<iostream>
#include<vector>
#include<string>
using namespace std; const int maxn =;
vector<int> pile[maxn];
int n;
void find_block(int a, int& p, int& h) { //找到木块所在的位置和高度(从0开始)
for (p = ; p < n; p++) {
for (h = ; h < pile[p].size(); h++) {
if (pile[p][h] == a) return;
}
}
} void clear_above(int p, int h) { //将p位置高度h以上的木块移回原位
for (int i = h + ; i < pile[p].size(); i++) {
int b = pile[p][i];
pile[b].push_back(b);
}
pile[p].resize(h + ); //保留 0-h高度的木块
} void pile_onto(int p, int p2,int h) { //将p位置处从h高度的木块全部移动到p2顶部
for (int i = h; i < pile[p].size(); i++) {
pile[p2].push_back(pile[p][i]);
}
pile[p].resize(h); //保留高度0~h-1的木块
} 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 ha, pa, hb, pb;
cin >> n;
for (int i = ; i < n; i++) pile[i].push_back(i);
string s1, s2;
int a, b;
while (cin >> s1 >> a >> s2 >> b) {
find_block(a, pa, ha);
find_block(b, pb, hb);
if (pa == pb) continue;
if (s1 == "move") clear_above(pa, ha);
if (s2 == "onto") clear_above(pb, hb);
pile_onto(pa, pb, ha);
}
print();
return ;
}

木块问题(UVa101)的更多相关文章

  1. 算法习题---5.2木块问题(UVa101)

    一:题目 输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为0~n-1的位置.现对这些木块进行操作,操作分为四种. .move a onto b:把木块a.b上的木块放回各自的原位,再把a放 ...

  2. 5_2 木块问题(UVa101)<vector的使用>

    [背景] 在计算机科学中的很多地方都会使用简单,抽象的方法来做分析和实验验究.比如在早期的规划学和机器人学的人工智能研究就利用一个积木世界,让机械臂执行操作积木的任务. 在这个问题中,你将在确定的规则 ...

  3. uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟

    挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...

  4. HDOJ 1330 Deck(叠木块-物理题啊!贪心算法用到了一点)

    Problem Description A single playing card can be placed on a table, carefully, so that the short edg ...

  5. Chocolate&&木块拼接&&Cards&& Wet Shark and Bishops

    B. Chocolate time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  6. CSS3实现3D木块旋转动画

    CSS3实现3D木块旋转动画,css3特效,旋转动画,3D,立体效果,CSS3实现3D木块旋转动画是一款迷人的HTML5+CSS3实现的3D旋转动画. 代码下载:http://www.huiyi8.c ...

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

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

  8. UVA101 The Blocks Problem 题解

    题目链接:https://www.luogu.org/problemnew/show/UVA101 这题码量稍有点大... 分析: 这道题模拟即可.因为考虑到所有的操作vector可最快捷的实现,所以 ...

  9. nyoj 260-数数小木块 (打表)

    260-数数小木块 内存限制:64MB 时间限制:3000ms 特判: No 通过数:17 提交数:24 难度:1 题目描述: 在墙角堆放着一堆完全相同的正方体小木块,如下图所示: 因为木块堆得实在是 ...

随机推荐

  1. (转)IOS 的一些资源汇总

      UI界面类项目: Panoramagl —— 720全景展示 Panorama viewer library for iPhone, iPad and iPod touch MBProgressH ...

  2. Codeforces 894.D Ralph And His Tour in Binary Country

    D. Ralph And His Tour in Binary Country time limit per test 2.5 seconds memory limit per test 512 me ...

  3. python安装包下载

    加入python官网一次按照下图点击: 这个exe文件就下好了,然后再安装一下即可.

  4. HTML5 Web Notifications 桌面推送小记

    目录 简介 常用API 手动设置权限 简介 Web Notifications目前在w3c的协议中已经是"推荐"(REC:Recommendation)阶段,除了iE外,各大现代浏 ...

  5. nova-virt与libvirt

    源码版本:H版 nova通过nova/virt/driver.py中的ComputeDriver对底层虚拟化技术进行抽象,不同的虚拟化技术在nova/virt下有不同的目录,里面均有driver.py ...

  6. OpenCV---ROI(region of interest)和泛洪填充

    一:ROI 感兴趣区(Region of Interest,ROIs) 是图像的一部分,它通过在图像上选择或使用诸如设定阈值(thresholding) 或者从其他文件(如矢量> 转换获得等方法 ...

  7. 最常用的8款 PHP 调试工具,你用过吗?

    Web 开发并不是一项轻松的任务,有超级多服务端脚本语言提供给开发者,但是当前 PHP 因为具有额外的一些强大的功能而越来越流行.PHP 是最强大的服务端脚本语言之一,同时也是 Web 开发者和设计者 ...

  8. 【Hadoop】大数据时代,我们为什么使用hadoop

    博客已转移,请借一步说话.http://www.daniubiji.cn/archives/538 我们先来看看大数据时代, 什么叫大数据,“大”,说的并不仅是数据的“多”!不能用数据到了多少TB , ...

  9. placeholder样式设置

    在input框中有时想将输入的字和placeholder设为不同的颜色或其它效果,这时就可以用以下代码来对placeholder进行样式设置了. ::-webkit-input-placeholder ...

  10. c语言学习笔记.预处理.#ifndef

    #ifndef -> if not define 配合 #endif使用 在h头文件中使用,防止重复包含和编译. 也可以用条件编译来实现. 例如: 编写头文件 test.h 在头文件开头写上两行 ...