算法竞赛入门经典第五章例题5-2 The Blocks Problem UVA - 101
2018-01-07 13:07
363 查看
https://vjudge.net/problem/UVA-101
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; #pragma warning(disable:4996) const int maxn = 30; vector<int> A[maxn];//A[k]=位置k处的状态 int index[maxn];//index[k]=编号k的木块在的位置 void ini(int x) { int k = index[x], b; while ((b=A[k].back()) != x) index[b] = b,A[b].push_back(b),A[k].pop_back(); } int main() { #ifdef _DEBUG freopen("in", "rb", stdin); //freopen("out", "wb", stdout); #endif // _DEBUG int N,a,b; cin >> N; for (int i = 0; i < N; ++i) index[i] = i, A[i].push_back(i); string s, t; while (cin >> s && s[0] != 'q') { cin >> a >> t >> b; if (index[a] == index[b]) continue; if (s[0] == 'm') ini(a); if (t[1] == 'n') ini(b); int k = index[a]; auto it = find(A[k].begin(),A[k].end(), a); while( it != A[k].end()) { A[index[b]].push_back(*it); index[*it] = index[b]; it = A[k].erase(it); } } for (int i = 0; i < N; ++i) { printf("%d:", i); for (auto x : A[i]) printf(" %d", x); printf("\n"); } }
相关文章推荐
- 例题 5-2 木块问题 (The Blocks Problem) UVa 101
- 算法竞赛入门经典第五章例题5-1 Where is the Marble? UVA - 10474
- 假期训练——The Blocks Problem UVA - 101 模拟
- 算法竞赛入门经典第五章例题5-9 Database UVA - 1592
- The Blocks Problem UVA 101 模拟
- The Blocks Problem UVA - 101
- 算法竞赛入门经典第五章例题5-8 Unix ls UVA - 400
- 《算法竞赛入门经典2ndEdition 》例题5-2 木块问题(The Blocks Problem, Uva101)
- uva101(The Blocks Problem)-线性表+模拟
- UVA 101 (暑期-线性表-D-The Blocks Problem)
- The Blocks Problem ——Uva 101
- 经典第五章例5.2 UVA 101 The Blocks Problem(vector的基础应用)
- 例题5-2 UVa101 The Blocks Problem(STL:vector)
- The Blocks Problem UVA - 101
- 算法竞赛入门经典第五章例题5-3 Andy's First Dictionary UVA - 10815
- UVa 101 - The Blocks Problem
- 算法竞赛入门经典第五章例题5-4 Ananagrams UVA - 156
- The Blocks Problem UVA - 101
- UVA 101 The blocks problem 例题整理
- The Blocks Problem UVA - 101