PKU 1208 The Blocks Problem(模拟+list应用)
题目大意:原题链接
关键是正确理解题目意思
首先:介绍一下list容器的一些操作:参考链接
list<int> c1;
c1.unique(); 去重。
c1.reverse(); 反转链表。
c1.insert(pos,num); 在pos位置插入元素num。
c1.insert(pos,n,num);在pos位置插入n个元素num。
c1.assign(n,num); 将n个num拷贝赋值给链表c。
c1.sort(); 将链表排序(默认升序)。
c1.swap(c2); 将c1和c2交换。
c1.remove(num); 删除链表中值为num的元素。
c1.push_back(num); 在末尾位置添加一个元素。
c1.pop_back(); 删除最末尾的元素。
c1.push_front(num); 在开始位置添加一个元素。
c1.pop_front(); 删除第一个元素。
c1.erase(pos); 删除pos位置的元素。
按要求模拟木块移动:有n(0<n<25)块block以及5种操作:
move a onto b:在将a搬到b所在的那堆积木上之前,先把a和b上方的积木放回原來的位置
move a over b:在将a搬到b所在的那堆积木上之前,先把a上方的积木放回原來的位罝
pile a onto b : 在将a搬到b所在的那堆积木上之前,先将b上方的积木放回原来的位置
pile a over b : 将包括a本身和a上方的积木一起搬到到b所在的那堆上方(以上三种也都包括a)
quit:结束命令,以上四个操作中,若a和b在同一堆中,则不进行任何操作。
注意:
1.当一个位置的初始箱子移走之后,因为这里没有箱子,其他箱子也不会移到这个位置。
2.数组p[id]=jd表示编号为id的木块放在编号为jd的木块上面
3.void Move(int x,int y)函数中为了保证移动后保持原来的顺序(包括最下方的木块)而利用一个临时变量
#include<list>
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
list<int> temp;
list<int> lis[];
int p[]; void Recover(int x)
{
int id=p[x];
while(lis[id].back()!=x){
int t=lis[id].back();
p[t]=t;//细节
lis[t].push_back(t);
lis[id].pop_back();
}
}
void Move(int x,int y)
{
int id=p[x],jd=p[y];
while(lis[id].back()!=x){
temp.push_back(lis[id].back());
lis[id].pop_back();
}
p[x]=jd;//细节
lis[jd].push_back(lis[id].back());//正确理解题意保持原来的的顺序
lis[id].pop_back();//清理干净
while(!temp.empty()){
lis[jd].push_back(temp.back());
p[temp.back()]=jd;
temp.pop_back();
}
} int main()
{
int n,a,b;
string str1,str2;
scanf("%d",&n);
for(int i=;i<n;i++){
p[i]=i;
lis[i].push_back(i);
}
while(cin>>str1&&str1!="quit"){
cin>>a>>str2>>b;
if(str1=="move"&&str2=="onto"){
Recover(a);
Recover(b);
Move(a,b);
}
else if(str1=="move"&&str2=="over"){
Recover(a);
Move(a,b);
}
else if(str1=="pile"&&str2=="onto"){
Recover(b);
Move(a,b);
}
else if(str1=="pile"&&str2=="over")
Move(a,b);
}
for(int i=;i<n;i++){
printf("%d:",i);
while(!lis[i].empty()){
printf(" %d",lis[i].front());
lis[i].pop_front();
}
printf("\n");
}
}
PKU 1208 The Blocks Problem(模拟+list应用)的更多相关文章
- POJ 1208 The Blocks Problem
The Blocks Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5397 Accepted: 231 ...
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
挺水的模拟题,刚开始题目看错了,poj竟然过了...无奈.uva果断wa了 搞清题目意思后改了一下,过了uva. 题目要求模拟木块移动: 有n(0<n<25)快block,有5种操作: m ...
- POJ 1208 The Blocks Problem --vector
http://poj.org/problem?id=1208 晚点仔细看 https://blog.csdn.net/yxz8102/article/details/53098575 #include ...
- B -- POJ 1208 The Blocks Problem
参考:https://blog.csdn.net/yxz8102/article/details/53098575 https://www.cnblogs.com/tanjuntao/p/867892 ...
- The Blocks Problem(vector)
题目链接:http://poj.org/problem?id=1208 The Blocks Problem Time Limit: 1000MS Memory Limit: 10000K Tot ...
- 【UVA - 101】The Blocks Problem(vector+模拟)
The Blocks Problem Descriptions:(英语就不说了,直接上翻译吧) 初始时从左到右有n个木块,编号为0~n-1,要求实现下列四种操作: move a onto b: 把a和 ...
- UVa 101 The Blocks Problem Vector基本操作
UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...
- 木块问题(The Blocks Problem,Uva 101)
不定长数组:vector vector就是一个不定长数组.不仅如此,它把一些常用操作“封装”在了vector类型内部. 例如,若a是一个vector,可以用a.size( )读取它的大小,a.resi ...
- UVa 101 - The Blocks Problem(积木问题,指令操作)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
随机推荐
- mac zsh选择到行首的快捷键
Mac OS X 下zsh切换窗口的快捷键:Shift-Command-←. 移动到当前命令行的行首,使用快捷键[Ctrl][A].移动到当前命令行的行尾,使用快捷键[Ctrl[E].
- 在执行一行代码之前CLR做的68件事[The 68 things the CLR does before executing a single line of your code]
待翻译,原文地址:http://mattwarren.org/2017/02/07/The-68-things-the-CLR-does-before-executing-a-single-line- ...
- mysql远程连接授权
Mysql:is not allowed to connect to this MySQL server 如果你想连接你的mysql的时候发生这个错误: ERROR 1130: Host '192.1 ...
- Objective-C 成员变量
成员变量的访问权限 Objective-C中的成员变量有以下三种属性 public(外部及其子类可访问) protected(子类可访问,外部不可访问) private(外部及其子类不可访问) 默认情 ...
- galera安装之编译安装xtrabackup 2.2.11
----1.编译安装percona-xtrabackup yum -y install cmake gcc gcc-c++ libaio libaio-devel automake autoconf ...
- iOS开发之 -- NSStringFromSelector的使用
很多时候,我们要触发一个时间,需要设置点击时间,当然了,有很多,比如:按钮,手势,tableview和其他一些空间自带的点击方法, 还有一个就是NSStringFromSelector的使用,废话不多 ...
- AssetsManager 在ios更新失败解决方案
AssetsManager在安卓平台使用正常,但是到ios就不行了,最后发现是 cocos2d\cocos\network\CCDownloader-apple.mm中的 - (void)URLSes ...
- ios常用第三方库git下载地址
本文转载至 http://blog.csdn.net/cerastes/article/details/38348599 iOS第三方库下载常用git 1.FMDB https://github.co ...
- Delphi 编译/链接过程
- Babel6.x的安装
1.首先安装babel-cli(用于在终端使用babel) npm install -g babel-cli 2.然后安装babel-preset-es2015插件 npm install --sav ...