挺水的模拟题,刚开始题目看错了,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. UIView的autoresizingMask属性

    今天做相册列表的时候,发现有些 UITableViewController 属性不好记忆,然后就查找了一些资料.做一下备份. 在 UIView 中有一个autoresizingMask的属性,它对应的 ...

  2. xib上拖拽scrollview的自动布局方法

    http://www.cocoachina.com/ios/20150104/10810.html

  3. 1016. 部分A+B (15)

    正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...

  4. ASP.NET MVC- HtmlHelper的用法

    在ASP.NET MVC框架中没有了自己的控件,页面显示完全就回到了写html代码的年代.还好在asp.net mvc框架中也有自带的HtmlHelper和UrlHelper两个帮助类.另外在MvcC ...

  5. android ListView进阶

    ListView 1.在android 开发中很多时候都要用到ListView的这个控件的,但用这个控件的时候会遇到一些问题,如在ListView中有Button按钮,就需要将按钮的监听事件给分离出来 ...

  6. CString和string的互相转换

    CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); s ...

  7. Setting up Nutch 2.1 with MySQL to handle UTF-8

    原文地址: http://nlp.solutions.asia/?p=180 These instructions assume Ubuntu 12.04 and Java 6 or 7 instal ...

  8. Flex随笔

    -keep-generated-actionscript=true 默认的情况在flex中 对label进行字体加粗的时候,只能对英文的字体加粗,而中文的就不可以加粗: 为了能够使中文能够加粗,需要将 ...

  9. How to Send Information (String, Image, Record) Between Two Applications

    http://delphi.about.com/od/windowsshellapi/a/wm_copydata.htm here are many situation when you need t ...

  10. Xcode 6 模拟器路径

    原文地址:http://leancodingnow.com/xcode-6-simulator-folder/ 本文主要介绍一下Xcode 6的iOS模拟器的应用目录的变化. Xcode 5的iOS模 ...