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 ...
随机推荐
- 利用python模拟鼠标点击自动完成工作,提升你的工作效率!
没有什么能比学以致用让学习变得更有动力的了. 不知道大家在工作中有没有一些工作需要重复的点击鼠标,因为会影响到财务统计报表的关系,我们每个月底月初都要修改ERP中的单据日期,单据多的时候光修改就能让你 ...
- python中numpy矩阵运算操作大全(非常全)!
python中numpy矩阵运算操作大全(非常全) //2019.07.10晚python矩阵运算大全1.矩阵的输出形式:对于任何一个矩阵,python输出的模板是:import numpy as n ...
- Centos7忘记mysql的root用户密码
1.先停止mysql服务 [root@CentOS ~]# ps -ef | grep mysql root : pts/ :: /bin/sh /usr/local/mysql/bin/mysql ...
- golang 使用编译选项-H=windowsgui后,仍然输出log到console
大概原理: 略略略... if debug { modkernel32 := syscall.NewLazyDLL("kernel32.dll") procAllocConsole ...
- django 中从外界借助多个网站时 static 的存放和整理
在 模板之家中 前端页面直接上去抓取 可是遇到重复 或者 版本不统一 所以 在每个app下面建立自己的 stastic 在制作的html 页面上方 导入静态页面 {% load static ...
- Spring课程 Spring入门篇 7-3 advice扩展
课程链接: 1 解析 1.1 advice中aspect 切面传参 1.2 通知参数名称——argNames属性, 参数为 JoinPoint.ProceedingJoinPoint.JoinPoin ...
- 九十一、SAP中ALV事件之五,查看状态栏,工具栏和功能键等
一.我们按照说明,来到SE37功能模块,然后点击[转到]->[函数组]->[显示组] 二.按照说明输入SALV,点击勾选 三.点击主程序 四.点击主程序后,我们来到函数组页面,然后 五.我 ...
- 070-PHP数组相加
<?php $arr1=array('a','b','c'); //定义一个数组 echo '数组$arr1的信息:<br />'; print_r($arr1); //输出数组信息 ...
- 想要写好Synthesis Essay,学会审题很重要
很多留学生都不太愿意写synthesis essay,因为它的难度要比其他类型的essay要大得多.写一篇synthesis essay需要有能力消化相关的信息,通过语言组织之后再呈现.虽然这种写作技 ...
- 浅谈Python之sys.argv
(1)sys.argv是什么 sys模块为进入解释器维护或使用的变量,以及与解释器相关的函数提供了途径.sys.argv在脚本程序中扮演了这样一个角色:将命令行输入的参数作为一个list传入脚本程序, ...