一个大模拟!!!

总的来说就是碰到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. (next_permutation)Ignatius and the Princess II hdu102

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  2. Service Fabric基本概念: Node, Application, Service, Partition/Replicas

    作者:张鼎松 (Dingsong Zhang) @ Microsoft 在上一节中,为大家简明扼要的介绍了微软针对现代分布式系统在Azure上实现的相关服务组件.紧接上文内容,本节将为大家介绍Azur ...

  3. promise第一篇-简介

    1. 创建一个promise对象 var promise = new Promise(function(resolve, reject){ //异步处理 //处理结束后调用resolve或reject ...

  4. CISCO知识扫盲

    cisco知识扫盲 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.vlan简介 1.什么是VLAN 简称虚拟局域网.至于英语怎么写自行百度吧. VLAN的优势: 1>.广 ...

  5. SpringBoot使用redis缓存List<Object>

    一.概述 最近在做性能优化,之前有一个业务是这样实现的: 1.温度报警后第三方通讯管理机直接把报警信息保存到数据库 2.我们在数据库中添加触发器,(BEFORE INSERT)根据这条报警信息处理业务 ...

  6. mysql -- 逻辑语句

    1.if语句 delimiter \\ create procedure p1() begin declare i ; then ; elseif i = then ; else ; end if; ...

  7. CSS实现文字竖排排版

    CSS实现文字竖排 注意: writing-mode: vertical-rl;存在兼容性问题,在IE上正常,在谷歌的低版本上可能识别不了. 如果文字少的话考虑直接在每一个文字中间加一个<br& ...

  8. Linux记录-自动删除几天前的日志文件

    #!/bin/sh DDIR=/usr/local/appserver/tomcat_risk/logs/risk_manage find $DDIR -mtime +30 -name "* ...

  9. Thread的setDaemon(true)方法

    定义:守护线程--也称“服务线程”,在没有用户线程可服务时会自动离开.优先级:守护线程的优先级比较低,用于为系统中的其它对象和线程提供服务. 设置:通过setDaemon(true)来设置线程为“守护 ...

  10. Docker入门03——Container

    1 启动容器 1.1 新建并启动 1.2 启动已终止容器 2 后台运行 3 终止 4 进入容器 5 导入和导出 5.1 导出 5.2 导入 6 删除 1 启动容器 1.1 新建并启动 docker r ...