木块问题(UVa101)
题目具体描述见: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)的更多相关文章
- 算法习题---5.2木块问题(UVa101)
一:题目 输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为0~n-1的位置.现对这些木块进行操作,操作分为四种. .move a onto b:把木块a.b上的木块放回各自的原位,再把a放 ...
- 5_2 木块问题(UVa101)<vector的使用>
[背景] 在计算机科学中的很多地方都会使用简单,抽象的方法来做分析和实验验究.比如在早期的规划学和机器人学的人工智能研究就利用一个积木世界,让机械臂执行操作积木的任务. 在这个问题中,你将在确定的规则 ...
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...
- HDOJ 1330 Deck(叠木块-物理题啊!贪心算法用到了一点)
Problem Description A single playing card can be placed on a table, carefully, so that the short edg ...
- Chocolate&&木块拼接&&Cards&& Wet Shark and Bishops
B. Chocolate time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- CSS3实现3D木块旋转动画
CSS3实现3D木块旋转动画,css3特效,旋转动画,3D,立体效果,CSS3实现3D木块旋转动画是一款迷人的HTML5+CSS3实现的3D旋转动画. 代码下载:http://www.huiyi8.c ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
- UVA101 The Blocks Problem 题解
题目链接:https://www.luogu.org/problemnew/show/UVA101 这题码量稍有点大... 分析: 这道题模拟即可.因为考虑到所有的操作vector可最快捷的实现,所以 ...
- nyoj 260-数数小木块 (打表)
260-数数小木块 内存限制:64MB 时间限制:3000ms 特判: No 通过数:17 提交数:24 难度:1 题目描述: 在墙角堆放着一堆完全相同的正方体小木块,如下图所示: 因为木块堆得实在是 ...
随机推荐
- 如何调整Flash与div的相互位置
让flash置于DIV层之下的方法,让flash不挡住飘浮层或下拉菜单,让Flash不档住浮动对象或层的关键参数:wmode=opaque. 方法如下: 针对IE 在<object>< ...
- Codeforces Round #271 (Div. 2) D 简单dp
D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input ...
- Qt ------ 断开某对信号与槽的connect
QMetaObject::Connection dis; dis = connect(this,&TcpSocket::readyRead,this,&TcpSocket::readD ...
- uboot&kernel&system
- Python进行数据分析(二)MovieLens 1M 数据集
# -*- coding: utf-8 -*- """ Created on Thu Sep 21 12:24:37 2017 @author: Douzi " ...
- 使用纯 CSS 实现响应式的图片显示效果
有许多方法可以实现页面里图像的响应式显示(Responsive).然而,我碰到的所有方案都使用了JavaScript.这使我疑惑不用 JavaScript 实现图像响应是否可行. 我提出了下面纯CSS ...
- 【leetcode 简单】第三十七题 相交链表
编写一个程序,找到两个单链表相交的起始节点. 例如,下面的两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始相交. 注意: 如果两个 ...
- 我的Mac使用笔记
此篇记录mac使用相关的一些东西,不断更新中... 1.Mac 安装homebrew : https://brew.sh/index_zh-cn.html /usr/bin/ruby -e " ...
- NYOJ 231 Apple Tree (树状数组)
题目链接 描述 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in t ...
- 详解H5中的history单页面,手动实现单页面开发,细说h5单页面原理
就目前来看,前端的单页面开发占了很大一部分,一方面无刷新的切换增强了体验,并且浏览器记录依然存在,前进后退都没问题,在之前我们通地址栏中的hash改变来触发onhashchange方法来实现单页面应用 ...