UVa 101 (模拟) The Blocks Problem
2015-01-09 09:32
337 查看
题意:
有n个木块及n个木块堆,初始状态是第i个木块在第i个木块堆上。对应有四种操作,然后输出最终状态。
分析:
用一个vector<int>模拟一个木块堆,进行相应操作即可。
代码君
有n个木块及n个木块堆,初始状态是第i个木块在第i个木块堆上。对应有四种操作,然后输出最终状态。
分析:
用一个vector<int>模拟一个木块堆,进行相应操作即可。
#include <iostream> #include <cstdio> #include <vector> #include <string> using namespace std; const int maxn = 30; int n; vector<int> pile[maxn]; void find_block(int a, int& p, int& h) { for(int i = 0; i < n; ++i) for(int j = 0; j < pile[i].size(); ++j) if(pile[i][j] == a) { p = i; h = j; } } void clear_block(int p, int h) {//将第p堆高度为h的上方的木块归位 for(int i = h+1; i < pile[p].size(); ++i) { int k = pile[p][i]; pile[k].push_back(k); } pile[p].resize(h+1); } void move_onto(int pa, int h, int pb) {//将pa堆高度为h及以上的木块摞到pb堆上 for(int i = h; i < pile[pa].size(); ++i) pile[pb].push_back(pile[pa][i]); pile[pa].resize(h); } int main() { //freopen("in.txt", "r", stdin); int a, b; scanf("%d", &n); for(int i = 0; i < n; ++i) pile[i].push_back(i); string s1, s2; while(cin >> s1 >> a >> s2 >> b) { int pa, pb, ha, hb; find_block(a, pa, ha); find_block(b, pb, hb); if(pa == pb) continue; if(s2 == "onto") clear_block(pb, hb); if(s1 == "move") clear_block(pa, ha); move_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]); printf("\n"); } return 0; }
代码君
相关文章推荐
- Uva 101 - The Blocks Problem//简单模拟
- UVA 101 The Blocks Problem(模拟)
- uva 101 The Blocks Problem (基本算法-模拟)
- UVA101 The Blocks Problem(模拟)
- UVa 101 The Blocks Problem 【STL】【vector】【模拟】
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- UVA 101 - The Blocks Problem(模拟)
- UVA - 101 The Blocks Problem(STL,模拟)
- 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