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 ...
随机推荐
- 066、Java面向对象之以分步的方式实例化对象
01.代码如下: package TIANPAN; class Book { // 定义一个新的类 String title; // 书的名字 double price; // 书的价格 public ...
- 062、Java中的方法重载
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- System.Reflection.ReflectionTypeLoadException
引用了一个第三方dll, 此dll经过混淆,但是未签名. 然后,主程序无法反射了, 取消主程序的签名后正常反射.
- Eclipse新建Maven中创建src文件夹报The folder is already a source folder错误解决办法
问题: 解决办法:右击项目->Build Path->Configure Build Path选择(missing)文件夹remove,然后重新New Source Folder
- LeetCode实战练习题目 - Array
实战练习题目 - Array 盛最多水的容器 class Solution { public: int maxArea(vector<int>& height) { int res ...
- Gerrit部署成功后project下不显示clone地址
gerrit部署成功后使用admin账号登录,在project All-projects下不显示clone地址,新建仓库也不显示. 原因是:默认安装没有安装插件download-commands 安装 ...
- windows中git输错密码后不能修改问题
坑位 当使用git做代码管理的时候,如果仓库地址地选用的是https,在初始拉取代码时,需要输入账号和密码,如果不小心输错了,后续一直会验证失败,无法再重新更正账号信息 Why 因为git为了不让你每 ...
- 032-session函数
<?php $username="guest1"; if(isset($username)) { session_name($username); } echo " ...
- 097-PHP循环使用next取数组元素二
<?php function return_item($arr, $num = 0) { //定义函数 if ($num < 0) { end($arr); //将数组指针指向最后一个元素 ...
- H5页面跳到安卓APP和iosAPP
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { // window.webkit.messagehandlers是js的固定写法, ...