The Blocks Problem-UVA 101
来源:互联网 发布:西安软件测试招聘 编辑:程序博客网 时间:2024/06/06 06:32
题目:uva101
用STL的vector更容易一些
按照入门经典所写的代码如下:
#include<iostream>#include<set>#include<iterator>#include<string>#include<set>#include<typeinfo>#include<queue>#include<list>#include<algorithm>#include<cstdio>#include<cctype>#include<cstring>#include<map>#include<vector>#include<cstdlib>#include<cmath>#include<stack>#include<sstream>#include<iomanip>const int maxn = 30;using namespace std;vector<int>p[maxn];int n;void find(int a,int &pa,int &ha){ for(pa = 0;pa<n;pa++) for(ha = 0;ha<p[pa].size();ha++) if(p[pa][ha]==a)return; //寻找a所在的堆以及高度}void clear_above(int pa,int ha) //归为函数{ for(int i = ha+1;i<p[pa].size();i++) { int d = p[pa][i]; p[d].push_back(d); } p[pa].resize(ha+1);}void pile_onto(int pa,int ha,int pb) //移动函数{ for(int i =ha;i<p[pa].size();i++) p[pb].push_back(p[pa][i]); p[pa].resize(ha);}int main(){ int a,b; string s1,s2; scanf("%d",&n); for(int i = 0 ;i<n;i++)p[i].push_back(i); while(cin>>s1) { if(s1=="quit") break; cin>>a>>s2>>b; int pa,ha,pb,hb; find(a,pa,ha); find(b,pb,hb); if(pa==pb)continue; if(s1=="move"&&s2=="onto"){clear_above(pa,ha);clear_above(pb,hb);pile_onto(pa,ha,pb);} if(s1=="move"&&s2=="over"){clear_above(pa,ha);pile_onto(pa,ha,pb);} if(s1=="pile"&&s2=="onto"){clear_above(pb,hb);pile_onto(pa,ha,pb);} if(s1=="pile"&&s2=="over"){pile_onto(pa,ha,pb);} } for(int i =0;i<n;i++) { cout<<i<<":"; for(int j=0;j<p[i].size();j++) cout<<" "<<p[i][j]; cout<<endl; } return 0;}
书中四种情况被分类,从而显得很简练,但是初读也许会有点迷,不如按照题目要求,写出四种情况。
0 0
- 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
- 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
- 有关Unity3D在VR领域的优化措施
- MicroPython是未来?当Python遇上单片机!
- tensorflow:tf.nn.conv2d是怎样实现卷积的?
- 用matlab实现k-means聚类
- a:hover实例
- The Blocks Problem-UVA 101
- UILabel + UITextView 字间距和行间距
- 个人学习总结一机器学习入门(三)
- React Native 开发常见错误
- POJ 1178 Camelot 最短路 Floyd +枚举
- Node.js 定时器
- WKWebView 遇到过的坑
- nyoj-106-背包问题
- Java -- 泛型