UVA 101 The Blocks Problem【vector】
来源:互联网 发布:python2.6抓取网页数据 编辑:程序博客网 时间:2024/05/18 04:40
source:
点击打开链接
题意:有n个方块,四种操作:
1、move a onto b:把a和b上面的方块都放回原来的位置,然后把a放到b上面;
2、move a over b:把a上面的放回原来的位置,然后把a放在b所在的方块堆的上面;
3、pile a onto b :把b上面的放回原来的位置,然后把a和a上面的方块整体放到b上面;
4、pile a over b :把a和a上面的方块整体放到b所在堆的上面。
求经过若干步操作之后方块的分布情况
思路:可以用两种子函数简化:
1.ret_place(a)函数:将a上面的方块放回原来的位置
2.pileto(a,b)函数:将a和a上面的方块整体移到b上面
另外关于vector的基本用法之前博文里总结过,见点击打开链接
代码如下:
#include<stdio.h>#include<vector>using namespace std;int belong[26],n;vector<int> seq[26];void ret_place(int a){ int t=belong[a]; int i=seq[t].size(); while(seq[t][--i]!=a) { int temp=seq[t][i]; belong[temp]=temp; seq[temp].push_back(temp); seq[t].pop_back(); }}void pileto(int a,int b){ int t=belong[a]; int temp[26],j=0; for(int i=seq[t].size()-1;seq[t][i]!=a;i--) { temp[j++]=seq[t][i]; seq[t].pop_back(); } temp[j]=a; seq[t].pop_back(); t=belong[b]; for(;j>=0;j--) { belong[temp[j]]=t; seq[t].push_back(temp[j]); }}int main(){ char oper[8],type[8]; int a,b; scanf("%d",&n); for(int i=0;i<n;i++) belong[i]=i; for(int i=0;i<n;i++) { seq[i].clear(); seq[i].push_back(i); } while(scanf("%s",oper) && oper[0]!='q') { scanf("%d %s %d",&a,type,&b); if(a==b || belong[a]==belong[b]) continue; if(oper[0]=='m') if(type[1]=='n') { ret_place(a); ret_place(b); } else ret_place(a); else if(type[1]=='n') ret_place(b); pileto(a,b); } for(int i=0;i<n;i++) { printf("%d:",i); for(int j=0;j<seq[i].size();j++) printf(" %d",seq[i][j]); printf("\n"); } return 0;}
阅读全文
0 0
- UVA - 101 The Blocks Problem vector
- UVA 101 The Blocks Problem【vector】
- UVa 101 The Blocks Problem(vector)
- UVA 101 The Blocks Problem【vector】
- UVa 101 The Blocks Problem Vector基本操作
- Uva 101 The Blocks Problem(不定长数组 vector)
- UVa 101 The Blocks Problem 【STL】【vector】【模拟】
- uva 101 the blocks problem
- uva 101 the blocks problem
- UVa 101 The Blocks Problem
- uva 101 - The Blocks Problem
- uva 101 - The Blocks Problem
- UVa 101 - The Blocks Problem
- uva 101 - The Blocks Problem
- UVa 101 - The Blocks Problem
- Uva-101-The Blocks Problem
- Uva 101 - The Blocks Problem
- uva 101 The Blocks Problem
- Spark2.2内核剖析
- Android AndroidStudio 项目结构
- Linux权限管理(基本权限、默认权限、
- Cookie与Session
- imagesLoaded-检测图片是否正确加载的js插件
- UVA 101 The Blocks Problem【vector】
- source insight 4.0 win10 无法复制
- Andrew Ng机器学习课程笔记--week6
- Java基本数据类型
- 关于BitBucket的一个坑:设置SSH
- 【java数据结构】快排非递归方式
- java中的路径
- 实战:战狼2票房数据分析——(2)票房数据构造及保存
- 逆序数