UVa 101 (模拟) The Blocks Problem
题意:
有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的更多相关文章
- 【UVA - 101】The Blocks Problem(vector+模拟)
The Blocks Problem Descriptions:(英语就不说了,直接上翻译吧) 初始时从左到右有n个木块,编号为0~n-1,要求实现下列四种操作: move a onto b: 把a和 ...
- 【例题5-2 UVA - 101】The Blocks Problem
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用vector模拟就好. resize的时候,只是把多余的清理掉. 原先的不会变的. [错的次数] 在这里输入错的次数 [反思] 在 ...
- UVa 101 The Blocks Problem Vector基本操作
UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
- UVa 101 - The Blocks Problem(积木问题,指令操作)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- Uva 101 -- the block problem
Uva 101 the block problem 题目大意: 输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为0~n-1的位置.现对这些木块进行操作,操作分为四种. 1.move a o ...
- POJ 1208 The Blocks Problem
The Blocks Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5397 Accepted: 231 ...
- The Blocks Problem(vector)
题目链接:http://poj.org/problem?id=1208 The Blocks Problem Time Limit: 1000MS Memory Limit: 10000K Tot ...
- UVA 10245 The Closest Pair Problem 最近点问题 分治算法
题意,给出n个点的坐标,找出两点间最近的距离,如果小于10000就输出INFINITY. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
随机推荐
- centos6.4下安装freetds使php支持mssql
centos版本:6.4 php版本5.3.17 没有安装之前的情况:nginx+php+mysql+FPM-FCGI 接下来安装步骤如下: 1.打开http://www.freetds.org/,进 ...
- MySQL存储引擎 InnoDB/ MyISAM/ MERGE/ BDB 的区别
MyISAM:默认的MySQL插件式存储引擎,它是在Web.数据仓储和其他应用环境下最常使用的存储引擎之一.注意,通过更改 STORAGE_ENGINE 配置变量,能够方便地更改MySQL服务器的默认 ...
- 另一个 SqlParameterCollection 中已包含 SqlParameter
出处:http://www.cnblogs.com/OldYongs/archive/2011/03/12/1982021.html#2742742 一般情况下,我们定义的一个SqlParameter ...
- 十、mysql事务的简介
1. myisam跟memory支持表级别锁定 BDB 页级锁定 Innodb 行级锁定 2.表锁(不是表嫂哈) lock table read //只读表锁,也就是说执行了这个锁后,锁内的操作只能为 ...
- 【css】web标准
网页主要由三部分组成:结构(Structure).表现(Presentation)和行为(Behavior) 结构重点理解: XHTML 1.应用形式 ccs+div 2000 2.基于xml 和x ...
- 10个优秀的Objective-C和iOS开发在线视频教程
如果你自己开发iOS应用,你肯定会发现网上有很多资源.学习编程的一个最好的方法就是自己写代码,而开始写代码的最快的方式就是看其他人怎么写.我们从海量视频和学习网站中整理出了我 如果你自己开发iOS应用 ...
- 【IOS】分享下近一年IOS开发的经验总结
从上个暑假末到现在,自己做IOS开发也快一年了.从一开始的什么都不知道,到现在大多事都能搭上一两手,期间经历了很多事情.下面来和大家分享一下心得和感触. 1.现在移动领域的知识更新的很快,无论是IOS ...
- css tricks
http://css-tricks.com/styling-cross-browser-compatible-range-inputs-css/ http://css-tricks.com/brows ...
- python字符串截取与替换的例子
python字符串截取与替换的多种方法 时间:2016-03-12 20:08:14来源:网络 导读:python字符串截取与替换的多种方法,以冒号分隔的字符串的截取方法,python字符串替换方法, ...
- HTML5 中的块级链接
英文叫做 “Block-level” links,我以为只有我厂那些鸟毛不知道,没想到不知道的还挺多, 需要普及一下. 最近看了 kejun 的 PPT 前端开发理论热点面对面:从怎么看,到怎么做?, ...