一个大模拟!!!

总的来说就是碰到move就要把a上面的全部放回原处。

如果碰到onto就要把b上面的全部放到原处。

因为move是只移动a一个,所以a上面的要归位,而pile是移一堆,所以不用。

onto是要和b贴在一起,所以b上面的要归位,而over是上方,不需要直接接触,所以不用。。

思路就是用栈来模拟,一开始就是n个栈。每个栈里都是一个元素,然后按照指令移,在这个栈里pop()掉它,在另一个栈里push()进去。。

分四种情况来做移动,每种情况处理方式不一样。要注意如果是一堆移过去,因为还是要按照这个顺序,多以要先把这一堆放到另一个数组,再按顺序push进去。

模拟完输出即可。。

CODE:

#include<iostream>
#include<cstdio>
#include<string>
#include<stack> using namespace std; stack <int> sta[100];
int t,num[100];
int res[100];
string m1,m2;
int p1,p2; int main () {
cin >> t;
getchar();
for( int i = 0; i < t ;i++) {
sta[i].push(i);
num[i] = i; }
while(1) {
cin >> m1;
if(m1 == "quit")
break;
cin >>p1 >>m2 >>p2;
if (num[p1] == num[p2])
continue;
if (m1 == "move" && m2 == "over") {
for (;sta[num[p1]].top() != p1; ) {
sta[ sta[num[p1]].top() ].push(sta[num[p1]].top());
num[sta[num[p1]].top()] = sta[num[p1]].top();
sta[num[p1]].pop();
}
sta[num[p2]].push(p1);
sta[num[p1]].pop();
num[p1] = num[p2];
}
if (m1 == "pile" && m2 == "over") {
int k = 0;
int temp[200];
for (;sta[num[p1]].top() != p1; ) {
temp[k++] = sta[num[p1]].top();
num[sta[num[p1]].top()] = num[p2];
sta[num[p1]].pop();
}
sta[num[p1]].pop();
temp[k] = p1;
num[p1] = num[p2];
for(int w = k ;w >= 0; w--)
sta[num[p2]].push(temp[w]);
}
if (m1 == "move" && m2 == "onto") {
for (;sta[num[p1]].top() != p1; ) {
sta[ sta[num[p1]].top() ].push(sta[num[p1]].top());
num[sta[num[p1]].top()] = sta[num[p1]].top();
sta[num[p1]].pop();
}
for (;sta[num[p2]].top() != p2; ) {
sta[ sta[num[p2]].top() ].push(sta[num[p2]].top());
num[sta[num[p2]].top()] = sta[num[p2]].top();
sta[num[p2]].pop();
}
sta[num[p2]].push(sta[num[p1]].top());
sta[num[p1]].pop();
num[p1] = num[p2];
}
if (m1 == "pile" && m2 == "onto") {
int k = 0;
int temp[200];
for (;sta[num[p1]].top() != p1; ) {
temp[k++] = sta[num[p1]].top();
num[sta[num[p1]].top()] = num[p2];
sta[num[p1]].pop();
}
sta[num[p1]].pop();
temp[k] = p1;
num[p1] =num[p2];
for (;sta[num[p2]].top() != p2; ) {
sta[ sta[num[p2]].top() ].push(sta[num[p2]].top());
num[sta[num[p2]].top()] = sta[num[p2]].top();
sta[num[p2]].pop();
}
for(int w = k ;w >= 0; w--)
sta[num[p2]].push(temp[w]); }
}
int j;
for(int i = 0;i < t;i++) {
cout << i <<":";
for( j = 0 ;!sta[i].empty();j++) {
res[j] = sta[i].top();
sta[i].pop();
}
for (j = j -1; j >= 0;j--)
cout<<" "<<res[j];
cout << endl;
}
return 0;
}

UVA101 【The Blocks Problem】的更多相关文章

  1. 【贪心】【TOJ4107】【A simple problem】

    Given three integers n(1≤n≤1018), m(1≤m≤105), k(1≤k≤1018). you should find a list of integer A1,A2,- ...

  2. 题解 P1001 【A+B Problem】

    #include<iostream> using namespace std; #define I int a,b; #define AK cin>>a>>b; # ...

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

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

  4. 【例题5-2 UVA - 101】The Blocks Problem

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用vector模拟就好. resize的时候,只是把多余的清理掉. 原先的不会变的. [错的次数] 在这里输入错的次数 [反思] 在 ...

  5. ZOJ Problem Set - 2297 Survival 【状压dp】

    题目:ZOJ Problem Set - 2297 Survival 题意:给出一些怪,有两个值,打他花费的血和能够添加的血,然后有一个boss,必须把小怪全部都打死之后才干打boss,血量小于0会死 ...

  6. 【牛客网】Whalyzh's Problem

    [牛客网]Whalyzh's Problem 每个\(b_{i,j}\)建一个点,认为选了\(b_{i,j}\)一定会选\(a_{i}\)和\(a_{j}\) 选了\(a_{i}\)的话会带了一个\( ...

  7. UVa101 The Blocks Problem(不定长数组vector)

    The Blocks Problem 书上的一道例题,代码思路比较清晰,可以看懂. 相关知识: 若a是一个vector,则: a.size():读取它的大小 a.resize():改变大小 a.pus ...

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

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

  9. 【故障•监听】TNS-12518、TNS-00517和 Linux Error:32:Broken pipe

    [故障|监听]TNS-12518.TNS-00517和 Linux Error:32:Broken pipe 1.1  BLOG文档结构图 1.2  前言部分 1.2.1  导读和注意事项 各位技术爱 ...

随机推荐

  1. Educational Codeforces Round 42 (Rated for Div. 2) E. Byteland, Berland and Disputed Cities

    http://codeforces.com/contest/962/problem/E E. Byteland, Berland and Disputed Cities time limit per ...

  2. idea中的language level 介绍

    language level 介绍 其他 IDE 没有看到类似 language level 的设置,所以这个功能应该算是 IntelliJ IDEA 特有的,可是 IntelliJ IDEA 官网也 ...

  3. 面向对象【day08】:类的特殊成员(一)

    本节内容 1.__doc__2.__module__和__class__3.__init__4.__del__5 .__call__6 .__dict__7 .__str__8 .__getitem_ ...

  4. win10重复安装

    使用大白菜启动盘装win10的时候,一直循环的重启然后设置,然后再重启,再设置.陷入了死循环. 解决办法:在自定义快捷键那个界面按 Ctrl+Shift+F3 .这是直接进入桌面的快捷键.

  5. JVM总结(四):JVM类加载机制

    这一节我们来总结一下JVM类加载机制.具体目录如下: 类加载的过程 类加载过程概括 说说引用 详解类加载全过程: 加载 验证 准备 解析 初始化 虚拟机把描述类的数据从Class文件加载到内存,并对数 ...

  6. java.io.Serializable 序列化问题【原】

    java.io.Serializable 序列化问题 Person.java package a.b.c; public class Person implements java.io.Seriali ...

  7. Neural Networks and Deep Learning 课程笔记(第三周)浅层神经网络(Shallow neural networks)

    3.1 神经网络概述(Neural Network Overview ) (神经网络中,我们要反复计算a和z,最终得到最后的loss function) 3.2 神经网络的表示(Neural Netw ...

  8. python 爆破

    python 爆破 #!/usr/bin/python #-*- coding: GB2312 -*- #author:loversorry import urllib2 import urllib ...

  9. HashMap分析及散列的冲突处理

    1,Hashing过程 像二分查找.AVL树查找,这些查找算法的时间复杂度为O(logn),而对于哈希表而言,我们一般说它的查找时间复杂度为O(1).那它是怎么实现的呢?这就是一个Hashing过程. ...

  10. 顺序列表(栈/队列等)ADT[C++]

    #include<iostream> using namespace std; //ADT template<class T> class SeqList{ public: / ...