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 导读和注意事项 各位技术爱 ...
随机推荐
- js模块化的两种规范AMD和CMD
AMD 规范在这里:https://github.com/amdjs/amdjs-api/wiki/AMDCMD 规范在这里:https://github.com/seajs/seajs/issues ...
- 即将上线的flume服务器面临的一系列填坑笔记
即将上线的flume服务器面临的一系列填坑笔记 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.flume缺少依赖包导致启动失败! 报错信息如下: 2018-10-17 ...
- GoLang基础数据类型--->数组(array)详解
GoLang基础数据类型--->数组(array)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Golang数组简介 数组是Go语言编程中最常用的数据结构之一.顾名 ...
- 在 github 新建一个文件夹
创建新文件的时候名字后面加个斜杠(/)就可以了.
- jenkins在windows平台自动化构建代码
jenkins服务端:centos6.8 客户端:windows server2012 windows10 工具:cwRsync 注:复制为jenkins工作目录到网站目录,无需服务端. 1.安装je ...
- js定时器整理(执行一次、重复执行)
在javascritp中,有两个关于定时器的专用函数,分别为: 1.倒计定时器:timename=setTimeout("function();",delaytime); 2.循环 ...
- SpringMVC学习笔记_02
1.springmvc对多视图的支持 (1)导入xml格式视图支持的jar包 注意:springmvc本身就支持xml格式,所以不用导入其他支持的jar包了. (2)在springmvc.xml中 ...
- mac系统如何在桌面新建文件(夹)
方法一:(终端方式,推荐) 1.在电脑上找到终端 2.指定你想要保存文件的路径,然后回车.例如桌面就是: cd desktop #或是 cd /Users/username/Desktop 有人问:假 ...
- react框架的状态管理
安装: cnpm install --save redux cnpm install --save react-redux 安装好后导入模块内容: impor {createStore} from ...
- AES加解密算法
直接粘代码,该类是基于微信公众号消息加密解密所提供的PHP DEMO改造而来,目前使用于彬彬大学APP接口token校验中. php的mcrypt 扩展已经过时了大约10年,并且用起来很复杂.因此它被 ...