我是靠谱客的博主 不安荷花,这篇文章主要介绍UVa101 - The Blocks Problem,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//UVa101 - The Blocks Problem #include<iostream> #include<cstdio> #include<string> #include<vector> using namespace std; const int maxn = 30; int n; vector<int>pile[maxn]; //找木块a所在的pile和height,以引用的形式返回调用者 void find(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; } //把p堆高度为h的木块上方的所有木块移回原位 void back(int p, int h){ for(int i = h+1; i < pile[p].size(); i++){ int b = pile[p][i]; pile[b].push_back(b); } pile[p].resize(h+1); } //把p堆高度为h及其上方的木块整体移动到p2的顶部 void doing(int p, int h, int p2){ for(int i = h; i< pile[p].size(); i++) pile[p2].push_back(pile[p][i]); pile[p].resize(h); } int main(){ int a, b; cin >> n; string s1, s2; for(int i = 0; i < n; i++) pile[i].push_back(i); while(cin >> s1 && s1 != "quit"){ cin >> a >> s2 >> b; int pa, pb, ha, hb; find(a, pa, ha); find(b, pb, hb); if(pa == pb) continue; if(s2 == "onto") back(pb, hb); if(s1 == "move") back(pa, ha); doing(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; } /* 10 move 9 onto 1 move 8 over 1 move 7 over 1 move 6 over 1 pile 8 over 6 pile 8 over 5 move 2 over 1 move 4 over 9 quit 0: 0 1: 1 9 2 4 2: 3: 3 4: 5: 5 8 7 6 6: 7: 8: 9: */

转载于:https://www.cnblogs.com/gwj1314/p/9444930.html

最后

以上就是不安荷花最近收集整理的关于UVa101 - The Blocks Problem的全部内容,更多相关UVa101内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部