挺水的模拟题,刚开始题目看错了,poj竟然过了。。。无奈。uva果断wa了

搞清题目意思后改了一下,过了uva。

题目要求模拟木块移动:

有n(0<n<25)快block,有5种操作:

move a onto b  在将a搬到b上之前,先把a和b上的积木放回原來的位置

move a over b在将a搬到b所在的那堆积木上前,先把a上的积木放回原來的位罝

pile a onto b 将包括a本身和上方的积木一起放到b上,在放之前b上方的积木放回原来的位置

pile a over b 将包括a本身和上放的积木一起搬到到b所在的那堆上

quit结束命令,前四个动作中若ab在同一堆中,则不做改变。

我用的vector模拟,其实用线性表也不错的,纯当练习stl了。

代码:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int>::iterator VI; vector <int> v[30];
int rec[30];
int n; VI Find(vector <int> &v, int num) {
for (VI i = v.begin(); i != v.end(); i++)
if (*i == num) {
return i;
}
return v.begin() - 1;
}//find num in vector v void check() {
/* for (int i = 0; i < n; i++)
cout << rec[i] << ' ';
cout << endl;
*/
for (int i = 0; i < n; i++) {
cout << i << ':';
if (v[i].empty()) {
cout << endl;
continue;
}
cout << ' ' << v[i][0];
for (int j = 1; j < v[i].size(); j++)
cout << ' ' << v[i][j];
cout << endl;
}
} int main() {
cin >> n;
for (int i = 0; i < n; i++) {
v[i].clear();
v[i].push_back(i);
rec[i] = i;
}//for init
string ts;
int tn1, tn2;
while (cin >> ts && ts != "quit") {
if (ts == "move") {
cin >> tn1 >> ts >> tn2;
if (rec[tn1] == rec[tn2]) continue;
while (v[rec[tn1]].back() != tn1) {
rec[v[rec[tn1]].back()] = v[rec[tn1]].back();
v[v[rec[tn1]].back()].push_back(v[rec[tn1]].back());
v[rec[tn1]].pop_back();
}
v[rec[tn1]].pop_back();
if (ts == "onto") {
VI i;
while (v[rec[tn2]].back() != tn2) {
rec[v[rec[tn2]].back()] = v[rec[tn2]].back();
v[v[rec[tn2]].back()].push_back(v[rec[tn2]].back());
v[rec[tn2]].pop_back();
}
v[rec[tn2]].push_back(tn1);
rec[tn1] = rec[tn2];
}//move onto
else {
v[rec[tn2]].push_back(tn1);
rec[tn1] = rec[tn2];
}//move over
}
else {
cin >> tn1 >> ts >> tn2;
if (rec[tn1] == rec[tn2]) continue;
if (ts == "onto") {
while (v[rec[tn2]].back() != tn2) {
rec[v[rec[tn2]].back()] = v[rec[tn2]].back();
v[v[rec[tn2]].back()].push_back(v[rec[tn2]].back());
v[rec[tn2]].pop_back();
}
VI pos1 = Find(v[rec[tn1]], tn1), pos2 = Find(v[rec[tn2]], tn2);
int tmp[25], cnt = 0;
for (VI i = v[rec[tn1]].end() - 1; i >= pos1; i--) {
// cout << "*i=" << *i << endl;
tmp[cnt++] = *i;
v[rec[tn1]].erase(i);
rec[*i] = rec[tn2];
}
for (int i = 0; i < cnt / 2; i++) {
int t = tmp[i];
tmp[i] = tmp[cnt - i - 1];
tmp[cnt - i - 1] = t;
}
v[rec[tn2]].insert(pos2 + 1, tmp, tmp + cnt); }//pile onto
else {
VI pos1 = Find(v[rec[tn1]], tn1);
int tmp[25], cnt = 0;
for (VI i = v[rec[tn1]].end() - 1; i >= pos1; i--) {
// cout << "*i=" << *i << endl;
tmp[cnt++] = *i;
v[rec[tn1]].erase(i);
rec[*i] = rec[tn2];
}
for (int i = 0; i < cnt / 2; i++) {
int t = tmp[i];
tmp[i] = tmp[cnt - i - 1];
tmp[cnt - i - 1] = t;
}
v[rec[tn2]].insert(v[rec[tn2]].end(), tmp, tmp + cnt);
}//pile over
}
}//while
check();
return 0;
}

uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟的更多相关文章

  1. POJ 1208 The Blocks Problem

    The Blocks Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5397   Accepted: 231 ...

  2. POJ 1208 The Blocks Problem --vector

    http://poj.org/problem?id=1208 晚点仔细看 https://blog.csdn.net/yxz8102/article/details/53098575 #include ...

  3. B -- POJ 1208 The Blocks Problem

    参考:https://blog.csdn.net/yxz8102/article/details/53098575 https://www.cnblogs.com/tanjuntao/p/867892 ...

  4. PKU 1208 The Blocks Problem(模拟+list应用)

    题目大意:原题链接 关键是正确理解题目意思 首先:介绍一下list容器的一些操作:参考链接 list<int> c1; c1.unique();              去重. c1.r ...

  5. The Blocks Problem(vector)

    题目链接:http://poj.org/problem?id=1208 The Blocks Problem Time Limit: 1000MS   Memory Limit: 10000K Tot ...

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

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

  7. UVa 101 The Blocks Problem Vector基本操作

    UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...

  8. UVa 101 - The Blocks Problem(积木问题,指令操作)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  9. 【UVA - 101】The Blocks Problem(vector+模拟)

    The Blocks Problem Descriptions:(英语就不说了,直接上翻译吧) 初始时从左到右有n个木块,编号为0~n-1,要求实现下列四种操作: move a onto b: 把a和 ...

随机推荐

  1. iOS面向编码|iOSVideoToolbox:读写解码回调函数CVImageBufferRef的YUV图像

    iOS面向编码|iOSVideoToolbox:读写解码回调函数CVImageBufferRef的YUV图像 本文档基于H.264的解码,介绍读写Video Toolbox解码回调函数参数CVImag ...

  2. POJ3321 Apple Tree (树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16180   Accepted: 4836 Descr ...

  3. 利用一些码农Trick去搞一搞G和T的单词

    根据自然语言处理中的Zipf统计定律,在自然语言的语料库里,一个单词出现的频率与它在频率表里的排名成反比.因此,我们有理由认为,可以根据这个频率表进行一下排序,以及purning.由于精力有限,没有足 ...

  4. 汇编语言程序入门实验二:在dos下建立子目录操作

    汇编语言程序入门实验二:在dos下建立子目录操作 1,背景 在读此文,并读懂前,建议读者先阅读这两篇博客 1,在dos环境下汇编语言程序设计入门(输出hello world)和masm32的下载.安装 ...

  5. struts2 下载时报java.lang.IllegalStateException

    -----------------------------------------struts2 下载时报java.lang.IllegalStateException---------------- ...

  6. android 简易时间轴(实质是ListView)

    ListView的应用 1.在很多时候是要用到时间轴的,有些处理的时间轴比较复杂,这里就给出一个比较简单的时间轴,其实就是ListView里面的Item的设计. 直接上代码: ListView,ite ...

  7. Chrome developer tool:本人钟爱的 console、Network 功能简谈

    在最开始时,本人调试查看网页,一直用的是 firefox 的 firebug 插件,并没有使用 chrome 的 developer tool .只不过,在日常生活使用过程中,一直使用的是 chrom ...

  8. 钉钉开发笔记(3)MySQL的配置

    最近在编写web的过程中,经常需要与后台工作人员互动.由于比较麻烦.没有效率. 就果断的请教了,公司的后台大牛,学习下数据库的一些简单操作,现在就把利用MySQL连接服务器, 进行可视化操作的简单步骤 ...

  9. hdu4291之矩阵快速幂

    A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. Hadoop伪分布配置与基于Eclipse开发环境搭建

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...