挺水的模拟题,刚开始题目看错了,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. KVM 虚拟机命令行安装配置

    KVM作为linux内核的一部分,有着无法比拟的优势,相信KVM的大范围企业应用指日可待. 一.KVM宿主服务器环境配置 1. 查看CPU是否支持VT技术 cat /proc/cpuinfo | eg ...

  2. cf754 B. Ilya and tic-tac-toe game

    呵呵呵,这个题简直是一直在乱做,真是最近太弱了 #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL long l ...

  3. Cache 工具类

    package com.thinkgem.jeesite.common.utils; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheM ...

  4. jsp与El,jstl知识点总结归纳

    jsp与El,jstl知识点总结归纳 jsp部分 一.jsp的三大指令 page ,include,taglib 1.jsp中的page指令 <% page %>-设置jsp 例如: &l ...

  5. 快速找到跟踪其他session产生的trc文件

    掌握该技术最根本的是需要搞清楚session跟踪文件存放的路径和生成跟踪文件的命名规则,不然,在已经存在成全上万trc文件的生产环境中,要想快速正确的找到跟踪其他SESSION产生的trc文件就如大海 ...

  6. Flume-NG + HDFS + HIVE 日志收集分析

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

  7. CodeForces 176B Word Cut dp

    Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...

  8. 初识ASP.NET---若干常见错误

    近期在学习ASP.NET的相关知识,期间遇到了一些错误,比較常见的错误总结了一下,希望此文能给ASP.NET刚開始学习的人一些帮助.同一时候记录这些错误也方便今后自己查看. 1.  GridView& ...

  9. getElementById getElementsByName 赋值

      <script type="text/javascript">                            window.onload=function ...

  10. Java SSH远程执行Shell脚本实现(转)

    前言 此程序需要ganymed-ssh2-build210.jar包(下载地址:http://www.ganymed.ethz.ch/ssh2/) 为了调试方便,可以将\ganymed-ssh2-bu ...