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. 纯暴力是会超时的,所以得另辟蹊径,用分治算法. 递归思路将点按坐标排序后,分成两块处理,最近的距离不是在两块中 ...
随机推荐
- 用端口映射在Apache中对每个项目分配端口,实现一个端口访问一个网站
映口映射的功能主要就是实际互联网可以访问当前进行映射的电脑,首先我们要进行路由器的端口映射,常见的开放端口有80,21,3389等,80是网站常用端口,21是FTP服务器常用端口,3389是远程桌面连 ...
- 生产场景NFS共享存储优化及实战
生产场景NFS共享存储优化: 1.硬件:sas/ssd磁盘,买多块,raid0/raid10,网卡好 2.NFS服务器端优化加all_squash,async /backup/NFS 192.168. ...
- 【刷机】Google Nexus s 蓝牙点击异常,无法启动,刷机解决方案
1 问题详述 手头上有一部Google Nexus S ,本机自带的输入法不好用,想下载其他的输入法,想用蓝牙传输一下apk文件,点了一下蓝牙开关想要打开蓝牙功能,但奇怪的情况出现了,手机一直重启, ...
- 删除_desktop.ini病毒文件
del h:\_desktop.ini /f/s/q/a/f 强制删除只读文件/s 从当前目录及其所有子目录栓出指定文件.显示正在删除的文件名/q 制定清音状态.不提示确认删除/a 按照属性来删除
- ems lite 客户端远程连接mysql server
在本地用ems客户端远程连接虚拟机上的mysql server,弹出客户端没有权限访问mysql server.使用下面方法进行设置:mysql> select host,user,passwo ...
- 2016 系统设计第一期 (档案一)MVC 和 Bootstrap 表单转换
bootstrap <form role="form"> <div class="form-group"> <label for= ...
- shell 后台执行命令
shell 后台执行命令方法: 1. nohup cmd & 后台会生成 nohup.out 文件 2.cmd >/路径/xx.log & 后台生成 xx. ...
- C#学习笔记---基础入门(二)
枚举 枚举是被命名的整型常数的集合:枚举类型的变量只有赋值后才能使用:不同枚举中的枚举值可以重名:可以自定义枚举值. enum Playstates { 跑, 跳,下滑,左转,右 ...
- iOS开发(1) WebView和HTML 显示
iOS 7 已经release了.现在学习iOS开发还是非常热门的.到处也有些团队在寻找iOS开发的人才. 那么,iOS开发.....省略了1万字.... HTML5 +CSS3+JS...再省略1万 ...
- html5 Doctor——教你规范使用html5标签
学习地址(英文资料):http://html5doctor.com/ http://www.w3.org/html/wg/drafts/html/master/text-level-semantics ...