UVa_The Blocks Problem(Vector)
2017-03-18 17:04
281 查看
题址:101 - The Blocks Problem
题意:
0 ~ n-1共n个木块,根据以下四种指令操作:move a onto b: 把 a, b 上方木块全部归位,把 a 放在 b 上面;
move a over b: 把 a 上方木块全部归位,把 a 放在 b 所在木块堆顶部;
pile a onto b: 把 b 上方木块全部归位,把 a 及 a 上方木块全部移到 b 上面;
pile a over b: 把 a 及 a 上面木块全部移到 b 所在木块顶部;
quit: 结束;
注意:a 和 b 在同一堆时忽略指令;
思路:
每一堆的堆长未知,所以用 vector;四个操作统一:
move: a 上方归位;
onto: b 上方归位;
pile over: a 及 a 上方移动到 b 上方;
代码:
#include <iostream> #include <cstdio> #include <vector> #include <string> using namespace std; const int maxn = 30; vector<int> block[maxn]; int n; void find_block(const int &a, int &p, int &h) { for(p = 0; p < n; p++) { for(h = 0; h < block[p].size(); h++) { if(block[p][h] == a) return; } } } void clear_above(int p, int h) { for(int i = h+1; i < block[p].size(); i++) { int b = block[p][i]; block[b].push_back(b); } block[p].resize(h+1); } void pile_onto(int pa, int ha, int pb) { for(int i = ha; i < block[pa].size(); i++) { block[pb].push_back(block[pa][i]); } block[pa].resize(ha); } void print() { for(int i = 0; i < n; i++) { printf("%d:", i); for(int j = 0; j < block[i].size(); j++) { // 不能是 2d, 如果是两位数就 PE printf("% d", block[i][j]); } printf("\n"); } } int main() { scanf("%d", &n); for(int i = 0; i < n; i++) block[i].push_back(i); string s1, s2; int a, b; // 不能用 scanf 来读 string 类 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(s1 == "move") clear_above(pa, ha); if(s2 == "onto") clear_above(pb, hb); pile_onto(pa, ha, pb); } print(); return 0; }
相关文章推荐
- UVa101-The Blocks Problem/STL-Vector的使用
- UVAThe blocks problem VECTOR的使用
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- UVa 101 The Blocks Problem(vector)
- Uva 101 The Blocks Problem(不定长数组 vector)
- UVa 101 The Blocks Problem Vector基本操作
- 经典第五章例5.2 UVA 101 The Blocks Problem(vector的基础应用)
- UVA 101 The Blocks Problem【vector】
- UVA 101 The Blocks Problem【vector】
- UVa 101 The Blocks Problem Vector基本操作
- C++ 不定长数组vector 例5-2木块问题(The Blocks Problem,UVa 101)
- 例题5-2 UVa101 The Blocks Problem(STL:vector)
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- UVa 101 The Blocks Problem 【STL】【vector】【模拟】
- uva 101 The Blocks Problem
- UVA101- The Blocks Problem
- uva 101 - The Blocks Problem
- uva 101 - The Blocks Problem
- Uva-101-The Blocks Problem
- UVa 101 - The Blocks Problem