木块问题 (using std::vector) UVA
来源:互联网 发布:在淘宝上买衣服还赚吗 编辑:程序博客网 时间:2024/06/01 09:06
题意:如果s2==”onto” 那么b上面全部归位,如果s1==”move”,那么a上面全部归位。然后再把所有a上面,包括a全部叠到b上面。
思路:模拟题。主要熟悉stl中 vector[maxn]的用法
/*If I get TLE , it is good.If I get AC,it's NICE !*/#include <bits/stdc++.h>using namespace std;typedef long long ll;const int INF=2147483647;const int MAXN=30;const ll mod=1e9+7;using namespace std;typedef long long ll;vector <int> pile[MAXN];int a,b;int n;void find_block(int a,int& p,int& h){ for(p=0; p<n; p++) { for( h=0; h<pile[p].size(); h++) { if(pile[p][h]==a) return ; } }}void clear_above(int p,int h){ for(int i=h+1; i<pile[p].size(); i++) { int x=pile[p][i]; pile[x].push_back(x); } pile[p].resize(h+1);}void pile_onto(int p1,int h,int p2){ for(int i=h; i<pile[p1].size(); i++) { pile[p2].push_back(pile[p1][i]); } pile[p1].resize(h);}int main(void){ cin >> n; for(int i=0; i<n; i++) pile[i].push_back(i); string s1,s2; while( cin >> s1 ) { if(s1=="quit") break; cin >> a >> s2 >> b; int pa,ha,pb,hb; find_block(a,pa,ha); find_block(b,pb,hb); if(pa==pb) continue; if(s2=="onto") clear_above(pb,hb); if(s1=="move") clear_above(pa,ha); pile_onto(pa,ha,pb); } for(int i=0; i<n; i++) { printf("%d:",i); for(int j=0; j<pile[i].size(); j++) { printf(" %d",pile[i][j]); } puts(""); } return 0;}
通过这道题我学到了什么??
1.vector 用法,有pile[i].push_back ,pile[i].size(),pile[i].resize();这三个比较重要的函数;(pile[i].empty是不是空,pile[i].pop_back删除最后一个)
2.对于一道模拟题,尽可能把相同的步骤在一个函数实现,这样就可以节约时间,代码量。
阅读全文
0 0
- 木块问题 (using std::vector) UVA
- 木块问题(vector)-UVa 101
- 紫书章五例二 木块问题 UVA 101(vector)
- Uva 101(vector的使用)木块问题
- UVA(木块问题)101
- 101UVa木块问题
- vector/using namespace std
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- Uva 101 the block problem 木块问题(算法竞赛经典入门)STL vector
- P2784 木块问题 本该用vector的.....
- 例题:木块问题(UVa 101)
- uva 101 木块问题 The Blocks Problem
- The Blocks Problem UVA 101 木块问题
- 木块问题(The Blocks Problem, UVa 101)
- using namespace std;问题
- 木块问题
- 木块问题
- std::vector 内存释放问题
- 线段树:HDU2795-Billboard(建树方式比较新奇)
- 处理hibernate懒加载出现的no session
- synchronized关键字
- 开门人和关门人 HDU
- 《剑指offer》二叉树的下一个节点
- 木块问题 (using std::vector) UVA
- 时间返回格式
- Servlet--获取Session对象
- Spring Boot 学习
- 链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,翻转后2→1→4→3→6→5。
- Linux Bash基本功能
- 设计模式之简单工厂模式
- 配置HAProxy实现Web负载均衡
- Java-Web org.apache.tomcat.util.http.fileupload.FileItem;