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 ...
随机推荐
- 删除C代码中的注释行【状态机】
今天在学ruby时遇到的一个经典的题目,一直都知道但从来没有实现过.呈上状态机,代码略.(写代码的时候还是需要注意一些小情况的)
- docker centos 镜像中安装python36详解!生成centos+python36的基础镜像
获取centos镜像docker pull centos:7.4.1708 启动并进入centos的容器docker run -i –t centos /bin/bash下载安装python编译环境依 ...
- SpringMVC之ModelAndView的用法
https://blog.csdn.net/qq30211478/article/details/78016155
- mysql 模糊查询中包含特殊字符查询
- poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
- 微信浏览器 UA
mozilla/5.0 (linux; android 5.1.1; mi note pro build/lmy47v) applewebkit/537.36 (khtml, like gecko) ...
- 安装phpmyadmin数据可视化
1.下载压缩包,并且解压 cd /usr/local/src wget https://files.phpmyadmin.net/phpMyAdmin/4.9.4/phpMyAdmin-4.9.4-a ...
- pr cs6安装教程
这是通过我自己实践操作,网上查询整理的安装流程: 安装 1.下载:http://www.smzy.com/smzy/smzy93225.html 2.断网,安装 如果到2%显示安装失败,在这里有详细解 ...
- python 小数相加报错 invalid literal for int() with base 10
for i in column1: x = int(i) s += xprint "sum:",s,"count:",len(column1)# round ( ...
- 五十八、SAP中常用预定义数据类型
一.SAP中常用预定义数据类型 注意事项如下: 1.默认的定义数据类型是CHAR. 2.取值的时候C型默认从左取,N型从右取,超过定义长度则截断. 3.C类型,可以赋值数值,也可以赋值字符,还可以混合 ...