挺水的模拟题,刚开始题目看错了,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. 在drupal7中动态的为某个内容类型添加字段

    $myField_name = "my_new_field_name"; if(!field_info_field($myField_name)) // check if the ...

  2. leetcode:Path Sum (路径之和) 【面试算法题】

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  3. Binding的Source从何而来?

    I. Binding to Object 1. Binding data using ObjectDataProvider AC:Let’s say there is a CLR based data ...

  4. [转]Android事件分发机制完全解析,带你从源码的角度彻底理解(上)

    Android事件分发机制 该篇文章出处:http://blog.csdn.net/guolin_blog/article/details/9097463 其实我一直准备写一篇关于Android事件分 ...

  5. 使用Canvas把照片转换成素描画

    原文:http://www.alloyteam.com/2012/07/convert-picture-to-sketch-by-canvas/ 腾讯的alloy team写的一个素描效果,挺不错的. ...

  6. JedisPool使用原理和源代码

    1,JedisPool的使用 <!-- 连接池的配置信息 --><beanid="jedisConfig"class="redis.clients.je ...

  7. (剑指Offer)面试题15:链表中倒数第k个结点

    题目: 输入一个链表,输出该链表中倒数第k个结点. 例如:链表中有6个结点,从头到尾依次为1,2,3,4,5,6,则该链表的倒数第3个结点为4. 链表结点定义: struct ListNode{ in ...

  8. 【JUnit】EasyMock用法总结

    使用EasyMock的总体步骤 1.生成Mock接口 IService mockService = EasyMock.createMock("name", IService.cla ...

  9. windows win7 win10 多系统启动菜单 多系统引导设置

    win键+R 输入msconfig 根据显示的程序设置(除非你看不懂文字)

  10. spring含参数 环绕通知demo

    题目:有一个懂得读心术的人需要完成两件事情:截听志愿者的内心感应和显示他们在想什么 <?xml version="1.0" encoding="UTF-8" ...