博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
阅读量:4552 次
发布时间:2019-06-08

本文共 2861 字,大约阅读时间需要 9 分钟。

挺水的模拟题,刚开始题目看错了,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 
#include
#include
using namespace std;typedef vector
::iterator VI;vector
v[30];int rec[30];int n;VI Find(vector
&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 vvoid 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;}

转载于:https://www.cnblogs.com/java20130723/p/3212119.html

你可能感兴趣的文章
GitHub for Windows提交失败“failed to sync this branch”
查看>>
linux 安装 git
查看>>
Margin
查看>>
完成登录与注册页面的前端
查看>>
centos 源码安装php7
查看>>
Log4j详细教程
查看>>
UVa-1368-DNA序列
查看>>
ConfigParser模块
查看>>
如何开发优质的 Flutter App:Flutter App 软件测试指南
查看>>
决胜Flutter 第一章 熟悉战场
查看>>
如何开发优质的 Flutter App:Flutter App 软件调试指南
查看>>
决胜经典算法之冒泡排序
查看>>
决胜经典算法之选择排序
查看>>
11、求二进制中1的个数
查看>>
【nodejs】让nodejs像后端mvc框架(asp.net mvc)一样处理请求--请求处理结果适配篇(7/8)...
查看>>
CodeForces 731A Night at the Museum
查看>>
MySQL 删除数据库
查看>>
JavaScript 字符串(String) 对象
查看>>
How to use VisualSVN Server and TortoiseSVN to host your codes and control your codes' version
查看>>
微信小程序picker组件 - 省市二级联动
查看>>