UVA101 【The Blocks Problem】
一个大模拟!!!
总的来说就是碰到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】的更多相关文章
- 【贪心】【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,- ...
- 题解 P1001 【A+B Problem】
#include<iostream> using namespace std; #define I int a,b; #define AK cin>>a>>b; # ...
- 【UVA - 101】The Blocks Problem(vector+模拟)
The Blocks Problem Descriptions:(英语就不说了,直接上翻译吧) 初始时从左到右有n个木块,编号为0~n-1,要求实现下列四种操作: move a onto b: 把a和 ...
- 【例题5-2 UVA - 101】The Blocks Problem
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用vector模拟就好. resize的时候,只是把多余的清理掉. 原先的不会变的. [错的次数] 在这里输入错的次数 [反思] 在 ...
- ZOJ Problem Set - 2297 Survival 【状压dp】
题目:ZOJ Problem Set - 2297 Survival 题意:给出一些怪,有两个值,打他花费的血和能够添加的血,然后有一个boss,必须把小怪全部都打死之后才干打boss,血量小于0会死 ...
- 【牛客网】Whalyzh's Problem
[牛客网]Whalyzh's Problem 每个\(b_{i,j}\)建一个点,认为选了\(b_{i,j}\)一定会选\(a_{i}\)和\(a_{j}\) 选了\(a_{i}\)的话会带了一个\( ...
- UVa101 The Blocks Problem(不定长数组vector)
The Blocks Problem 书上的一道例题,代码思路比较清晰,可以看懂. 相关知识: 若a是一个vector,则: a.size():读取它的大小 a.resize():改变大小 a.pus ...
- UVa 101 - The Blocks Problem(积木问题,指令操作)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 【故障•监听】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 导读和注意事项 各位技术爱 ...
随机推荐
- 2018.10.2浪在ACM 集训队第二次测试赛
2018.10.26 浪在ACM 集训队第二次测试赛 题目一览表 来源 考察知识点 A 1273 海港 NOIP 普及组 2016 差分数组+二分 B 1274 魔法阵 C 1267 金币 ...
- php 字符串截取函数
php自带的一个截取字符串的函数,只能处理英文,数字的不能截取中文混排的哦,有需要的朋友可以参考,后面的比较好用,第一个主要是给初学者学学用的. php //构造字符串 $str = "AB ...
- elasticsearch索引清理
只是记录两条简单的命令 查看所有的索引文件: curl -XGET http://localhost:9200/_cat/indices?v 删除索引文件以释放空间: curl -XDELETE ht ...
- centos7环境下安装mysql5.6-----解压安装包的方法
参考连接:https://blog.csdn.net/qq_17776287/article/details/53536761 linux下有很多服务,如何查看服务是否启动,以mysql为例子 使用命 ...
- linq总结系列(二)---Expression
一.linq中的表达式和表达式树 Linq中的表达式(Expression<TDel>)是强类型的lambda表达式,对Func和Action形式的委托做了一层封装. lambda表达式的 ...
- Networx蓝屏问题
本人系统win7专业版64位. 从5月底开始就时不时有蓝屏发生,而且可以说是没有任何征兆就"啪"的一下蓝了... 有时候是隔个四五天蓝屏一次,有时候一天都能蓝好几次,实在是让人恼火 ...
- mysql Mac终端操作
1.启动mysql :brew services start mysql 2.登陆mysql : mysql -u root -p mysql 命令. -u 后面接用户名 root超级管理 ...
- .NET面试题系列(十一)WinDbg、Perfmon
WinDbg 资料 https://www.cnblogs.com/sheng-jie/p/9503650.html https://www.cnblogs.com/yudongdong/p/9701 ...
- java实现网页验证码
Servlet: package cn.bdqn.servlet; import javax.imageio.ImageIO; import javax.servlet.ServletExceptio ...
- u-boot移植(六)---代码修改---串口
一.代码流程 1.1 串口代码 程序流程图如下: default_serial_console 执行的代码如下: 在JZ2440.H中有如下定义: 则执行结构体s3c24xx_serial0_devi ...