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. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
随机推荐
- PHP实现冒泡算法
<?php //php函数:count($arr)返回array的数值总数. function bubble_sort($arr){ for ($i = 6;$i > 0;$i --){ ...
- Spark Streaming揭秘 Day8 RDD生命周期研究
Spark Streaming揭秘 Day8 RDD生命周期研究 今天让我们进一步深入SparkStreaming中RDD的运行机制.从完整的生命周期角度来说,有三个问题是需要解决的: RDD到底是怎 ...
- 内容自适应UILabel
xcode 6.1 File-New-Project.. iOs-Application-Simple View Application 代码: - (void)viewDidLoad { [supe ...
- [ 转载]JAVA Socket超时浅析
JAVA Socket超时浅析 转载自 http://blog.csdn.net/sureyonder/article/details/5633647 套接字或插座(socket)是一种软件形 式的抽 ...
- 【POJ2104】kth num
You are working for Macrohard company in data structures department. After failing your previous tas ...
- C# Windows - RadioButton&CheckBox
RadioButton和CheckBox控件与Button控件有相同的基类,但它们的外观和用法大不相同. RadioButton显示为一个标签,左边是一个圆点,该点可以是选中或未选中.用在给用户提供两 ...
- Code for the Homework2 改进
1. 实现了到指定点各个关节的转角计算(多解性),并且所求解满足各个关节的最大角和最小角的限制条件. 2. 对方向向量进行了单位化,保证任意大小的向量都行 #include<iostream&g ...
- 扎克伯格谈Facebook创业过程
第一课:Facebook的产品研发 (1)不仅注重用户体验,更关注程序本身对社会和产品的是否有益,进而对产品做出调整 (2)以学校为标准作为群组来划分,就是对产品进行了思考后决定的,不管是直觉决定还是 ...
- c++ 小片段
void test_string() { string sen = "Connection will be closed if there is no " "read/w ...
- linux 命令 备忘
openssl rand -base64 32 随机数 date | md5sum data 日期 cal 日历 man -f man sync 数据同步写入磁盘 shutdown reboot ha ...