参考:https://blog.csdn.net/yxz8102/article/details/53098575
https://www.cnblogs.com/tanjuntao/p/8678927.html
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #define N 30 5 using namespace std; 6 int n,a,b; 7 string s,sa; 8 vector<int> v[N]; 9 void init() 10 { 11 int i; 12 for (i=0;i<N;i++) 13 { 14 v[i].clear(); 15 v[i].push_back(i); 16 } 17 } 18 void test() 19 { 20 cout<<s<<a<<sa<<b<<endl; 21 } 22 bool judge(int& ba,int& bb,int& ha,int& hb)//判断输入合法性 ,地址传参 23 { 24 int i,j; 25 for (i=0;i<n;i++) 26 { 27 for (j=0;j<v[i].size();j++) 28 { 29 if (v[i][j]==a) 30 { 31 ba=i; 32 ha=j; 33 } 34 if (v[i][j]==b) 35 { 36 bb=i; 37 hb=j; 38 } 39 } 40 } 41 if (ba==bb) 42 { 43 return true; 44 } 45 else 46 { 47 return false; 48 } 49 } 50 void clear(int block,int high) 51 { 52 int i; 53 for (i=high+1;i<v[block].size();i++) 54 { 55 int t=v[block][i]; 56 v[t].push_back(t); 57 } 58 v[block].resize(high+1); 59 } 60 void pile(int pba,int pbb,int pha,int phb) 61 { 62 int i; 63 for (i=pha;i<v[pba].size();i++) 64 { 65 v[pbb].push_back(v[pba][i]); 66 } 67 v[pba].resize(pha);//重设长度会删除后面元素~ 68 } 69 void show() 70 { 71 int i; 72 for (i=0;i<n;i++) 73 { 74 printf("%d:",i); 75 for (int j=0;j<v[i].size();j++) 76 { 77 printf(" %d",v[i][j]);//依据方便实用printf,cout!!! 78 } 79 printf(" "); 80 } 81 } 82 int main() 83 { 84 while (scanf("%d",&n)!=EOF) 85 { 86 int i; 87 init(); 88 while (cin>>s) 89 { 90 if (s=="quit") 91 { 92 break; 93 } 94 cin>>a>>sa>>b; 95 int ba,bb,ha,hb;//ba为a的堆,ha为a在堆的高度 96 if (judge(ba,bb,ha,hb))//多用传参形式,少用全局变量!!! 97 { 98 continue; 99 } 100 if (s=="move") 101 { 102 clear(ba,ha); 103 } 104 if (sa=="onto") 105 { 106 clear(bb,hb); 107 } 108 pile(ba,bb,ha,hb); 109 } 110 show(); 111 } 112 113 return 0; 114 }