一个大模拟!!!

总的来说就是碰到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. 包学会之浅入浅出Vue.js:结业篇(转)

    蔡述雄,现腾讯用户体验设计部QQ空间高级UI工程师.智图图片优化系统首席工程师,曾参与<众妙之门>书籍的翻译工作.目前专注前端图片优化与新技术的探研. 在第一篇<包学会之浅入浅出Vu ...

  2. shell中脚本变量和函数变量的作用域

    http://blog.csdn.net/ltx19860420/article/details/5570902 1. shell脚本中定义的变量是global的,其作用域从被定义的地方开始,到she ...

  3. (qsort)绝对值排序

    绝对值排序 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  4. 静态代码块、构造代码块、构造方法优先级(重点)-------java基础总结

    package com.mon11.day11; /** * 类说明 : * @author 作者 : chenyanlong * @version 创建时间:2017年11月11日 */ publi ...

  5. Java_myBatis_XML代理_延迟加载

    使用mybatis的延迟加载,需要两个步骤: 1.在全局配置文件中添加一下语句(lazyLoadingEnabled默认为false,aggressiveLazyLoading默认为true) < ...

  6. GO语言的进阶之路-go的程序结构以及包简介

    GO语言的进阶之路-go的程序结构以及包简介 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编辑,编译和运行 A,编辑 Go程序使用UTF-8编码的纯Unicode文本编写.大 ...

  7. AbstractQueuedSynchronizer同步队列与Condition等待队列协同机制

    概要: AQS维护了一个同步队列 Condition是JUC的一个接口,AQS的ConditionObject实现了这个接口,维护了一个等待队列(等待signal信号的队列) 线程调用reentran ...

  8. Bootstrap --对话框及提示框的处理和优化

    源模态框使用:http://www.runoob.com/bootstrap/bootstrap-modal-plugin.html <!-- 按钮触发模态框 --> <button ...

  9. float导致出现大面积空白

    float导致出现大面积空白,解决方法: *{ padding: 0; margin: 0; overflow:hidden; }

  10. oracle里的tns是什么意思

    参考http://www.jb51.net/article/44668.htm TNS简要介绍与应用 Oracle中TNS的完整定义:transparence Network Substrate透明网 ...