UVA-101 The Blocks Problem 栈模拟
终于AC了,这道题目去年寒假卡得我要死,最后一气之下就不做了。。。想想居然一年之久了,我本来都快忘了这道题了,最近发现白书的奥秘,觉得刘汝佳的题目真的相当练思维以及对代码的操作,决定又刷起题目来,这时候才想起这道题。
用栈进行模拟堆砖块,用个rec[]数组记录其现在所在的栈号,比较麻烦的是pile 操作,为了把a以及a以上的所有砖块都以原秩序放置于b砖块顶端,我用了个临时的栈进行存贮,然后再一个一个放到b栈上面。。这样就不会破坏秩序。。但是感觉这样做挺耗时的,原以为通不过,结果还是通过了。。。22ms,也不算太高吧。。不知道还有没有更好的pile方法
这个题目去年我都没想清楚题意,题目里面有个关键词 initial,意味着所有操作要还原的砖块都应该还原到它原本的位置,即 1还原到1号栈 2还原到2号栈,依次类推,因为根据题目的意思以及几大操作分析,一个栈要么就没元素,要么栈底元素就是栈号对应的元素,一旦移走了,栈必为空,一旦要还原,必定就把还原成最原始的样子
。一年了,觉得自己思维进步了一些,这是好事,继续加油!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#define N 35
using namespace std;
stack <int> arr[N];
int rec[N];
int n,a,b;
char ch1[],ch2[];
void solve()
{
int temp=arr[rec[a]].top();;
int t2=arr[rec[b]].top();
if (temp==t2) return;
if (ch1[]=='m' && ch2[]=='n')
{ while (temp!=a)
{
arr[temp].push(temp);
rec[temp]=temp;
arr[rec[a]].pop();
temp=arr[rec[a]].top();
} while (t2!=b)
{
arr[t2].push(t2);
rec[t2]=t2;
arr[rec[b]].pop();
t2=arr[rec[b]].top();
}
arr[rec[b]].push(a);
arr[rec[a]].pop();
rec[a]=rec[b];
return;
}
if (ch1[]=='m' && ch2[]=='v')
{ while (temp!=a)
{
arr[temp].push(temp);
rec[temp]=temp;
arr[rec[a]].pop();
temp=arr[rec[a]].top();
}
arr[rec[b]].push(a);
arr[rec[a]].pop();
rec[a]=rec[b];
return;
}
if (ch1[]=='p' && ch2[]=='n')
{ while (t2!=b)
{
arr[t2].push(t2);
rec[t2]=t2;
arr[rec[b]].pop();
t2=arr[rec[b]].top();
}
stack <int> q;
while (temp!=a)
{
q.push(temp);
arr[rec[a]].pop();
temp=arr[rec[a]].top();
}
arr[rec[b]].push(temp);
arr[rec[a]].pop();
rec[a]=rec[b];
while (!q.empty())
{
int tt=q.top();
q.pop();
rec[tt]=rec[b];
arr[rec[b]].push(tt);
}
return;
}
if (ch1[]=='p' && ch2[]=='v')
{
stack <int> q;
while (temp!=a)
{
q.push(temp);
arr[rec[a]].pop();
temp=arr[rec[a]].top();
}
arr[rec[b]].push(temp);
arr[rec[a]].pop();
rec[a]=rec[b];
while (!q.empty())
{
int tt=q.top();
q.pop();
rec[tt]=rec[b];
arr[rec[b]].push(tt);
}
}
}
void print()
{
for (int i=;i<n;i++)
{
printf("%d:",i);
stack<int> q;
while (!arr[i].empty())
{
int temp=arr[i].top();
q.push(temp);
arr[i].pop();
} while (!q.empty())
{
printf(" %d",q.top());
q.pop();
} putchar('\n');
}
}
int main()
{
scanf("%d",&n);
int i,j;
for (i=;i<n;i++){
arr[i].push(i);
rec[i]=i;
}
getchar();
while (scanf("%s",ch1))
{
if (ch1[]=='q')
break;
scanf("%d%s%d",&a,ch2,&b);
getchar();
solve();
}
print();
}
UVA-101 The Blocks Problem 栈模拟的更多相关文章
- UVa 101 The Blocks Problem Vector基本操作
UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...
- UVa 101 - The Blocks Problem(积木问题,指令操作)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVa 101 The Blocks Problem
题意:给出从左到右放置的n块木块(从0开始编号),再给出四种操作,再给出相应的操作,输出操作结束后每一堆木块的情况. 学习的紫书,因为每一堆的木块数是在发生变化的,所以用vector. 然后就是模拟几 ...
- UVa 101 - The Blocks Problem STL
题目:给你n个方块,有四种操作: .move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面: .move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上 ...
- Uva 101 -- the block problem
Uva 101 the block problem 题目大意: 输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为0~n-1的位置.现对这些木块进行操作,操作分为四种. 1.move a o ...
- ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)
Description ``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patie ...
- uvaoj 101 - The Blocks Problem(vector应用+技巧)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page= ...
- 【UVA - 101】The Blocks Problem(vector+模拟)
The Blocks Problem Descriptions:(英语就不说了,直接上翻译吧) 初始时从左到右有n个木块,编号为0~n-1,要求实现下列四种操作: move a onto b: 把a和 ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
随机推荐
- 017、Java中使用float型
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 009、MySQL取当前时间Unix时间戳,取今天Unix时间戳
#取Unix时间戳 SELECT unix_timestamp( ) ; #取今天时间戳 SELECT unix_timestamp( curdate( ) ); 显示如下: 不忘初心,如果您认为这篇 ...
- pig安装配置及实例
一.前提 1. hadoop集群环境配置好(本人hadoop版本:hadoop-2.7.3) 2. windows基础环境准备: jdk环境配置.esclipse环境配置 二.搭建pig环境 1.下载 ...
- DStream-05 updateStateByKey函数的原理和源码
Demo updateState 可以到达将每次 word count 计算的结果进行累加. object SocketDstream { def main(args: Array[String]): ...
- Spark的任务提交和执行流程概述
1.概述 为了更好地理解调度,我们先看一下集群模式的Spark程序运行架构图,如上所示: 2.Spark中的基本概念 1.Application:表示你的程序 2.Driver:表示main函数,创建 ...
- ruby资料
源码样例 链接: https://pan.baidu.com/s/1mh55bFM 密码: 6cjy 初级代码 链接: https://pan.baidu.com/s/1hschnUW 密码: 8n1 ...
- c++输出哈夫曼编码
#include<iostream> #include<string> using namespace std; struct huffTree { int parent; i ...
- JPA#复杂查询#自定义查询
编写自定义SQL基于下面信息:1. SpringData JPA 在为Repository接口生成实现的时候,会查找是否有 "接口名称"+"Impl"的类,如果 ...
- Mybatis中xml文件的时间段动态查询
- Bean 注解(Annotation)配置(2)- Bean作用域与生命周期回调方法配置
Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...