uva 101
来源:互联网 发布:在线教育cms 编辑:程序博客网 时间:2024/05/02 09:32
数据结构+模拟~
题意大概有N个木块,有4种操作。
move a onto b 将a木块和b木块上面的木块移回最开始的地方(这个最坑了,英语真心一开始木有看懂。)然后把a移到b上面。
move a over b 只把a木块上面的木块移到原来的位置,然后把a移到有b的一堆木块上即可(不论b是否是最上面)
pile a onto b 把a和a上面的一堆木块移动到b上面,b上面的木块移回到最初的位置。
pile a over b 把a上面的一堆移动到b上面的一堆。
AC代码:
#include<cstdio>#include<cstring>#include<stack>using namespace std;const int MAXN=30;int place[MAXN]; //存每个木块的位置stack<int>sta[MAXN]; //每一堆用栈表示void move_onto(int a,int b){ //模拟move onto的操作 while(sta[place[b]].top()!=b){ //将b上面的木块移回原来的位置,记得改变place,和清除已移动的木块 int temp=sta[place[b]].top(); sta[place[b]].pop(); sta[temp].push(temp); place[temp]=temp; } while(sta[place[a]].top()!=a){ int temp=sta[place[a]].top(); sta[place[a]].pop(); sta[temp].push(temp); place[temp]=temp; } sta[place[a]].pop(); place[a]=place[b]; sta[place[b]].push(a);}void move_over(int a,int b){ //只用移动a的,然后放在有b的堆上面 while(sta[place[a]].top()!=a){ int temp=sta[place[a]].top(); sta[place[a]].pop(); sta[temp].push(temp); place[temp]=temp; } sta[place[a]].pop(); place[a]=place[b]; sta[place[b]].push(a);}void pile_onto(int a,int b){ while(sta[place[b]].top()!=b){ int temp=sta[place[b]].top(); sta[place[b]].pop(); sta[temp].push(temp); place[temp]=temp; } int temp[MAXN],m=0; while(1){ //临时存一个数组将要移动的一堆记录,stack只能从顶端入,顶端出,要有中间过一次 temp[m++]=sta[place[a]].top(); sta[place[a]].pop(); place[temp[m-1]]=place[b]; if(temp[m-1]==a) break; } for(int i=m-1;i>=0;i--) sta[place[b]].push(temp[i]);}void pile_over(int a,int b){ int temp[MAXN],m=0; while(1){ temp[m++]=sta[place[a]].top(); sta[place[a]].pop(); place[temp[m-1]]=place[b]; if(temp[m-1]==a) break; } for(int i=m-1;i>=0;i--) sta[place[b]].push(temp[i]);}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); memset(place,0,sizeof(place)); int n; scanf("%d",&n); char a[6],b[6]; int c,d; for(int i=0;i<n;i++){ //初始化 place[i]=i; sta[i].push(i); } while(~scanf("%s",a)){ if(strcmp(a,"quit")==0) break; scanf("%d%s%d",&c,b,&d); if(place[c]==place[d]) continue; // printf("%s %d %s %d\n",a,c,b,d); if(strcmp(a,"move")==0 && strcmp(b,"onto")==0) move_onto(c,d); else if(strcmp(a,"move")==0 && strcmp(b,"over")==0) move_over(c,d); else if(strcmp(a,"pile")==0 && strcmp(b,"onto")==0) pile_onto(c,d); else if(strcmp(a,"pile")==0 && strcmp(b,"over")==0) pile_over(c,d); } int temp[MAXN]; for(int i=0;i<n;i++){ //输出时也要先存到临时数组里输出 int m=0; printf("%d:",i); while(!sta[i].empty()) {temp[m++]=sta[i].top();sta[i].pop();} for(int j=m-1;j>=0;j--) printf(" %d",temp[j]); printf("\n"); } return 0;}
0 0
- uva 101
- UVA 101
- UVA 101
- uva 101
- uva 101
- uva 101
- uva 101
- uva-101
- uva 101
- UVa 101
- UVa 101
- uva 101
- uva 101
- uva 101
- UVA 101
- uva 101
- uva 101
- UVA - 101
- linux下双网卡接收组播需要修改的参数
- AllWinnerTech A10添加驱动ko模块方法
- Java怎么解决浏览器的缓存问题
- LeetCode 题解(31): Longest Substring Without Repeating Characters
- ORACLE 数据库的级联查询 一句sql搞定(部门多级)
- uva 101
- 软件开发流程
- XMPP 资料收集
- 支持向量机(Support Vector machine)--libSvm中自定义核的扩展
- POJ 1703 Find them, Catch them (关系并查集)
- OpenCV去除背景方法详解
- 随机排列
- 电子商务网站的 10 个易用性规则
- VS2010下安装boost库