新汉诺塔
肯定是要先把大的摆好,然后再去摆小的,那如果大的不在最上面怎么办,就应该把所有小的都移到另外的那根柱子上,
所以就是一个不断递归的过程
但这其实是随机化贪心有hack数据的,所以...
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 const int maxn=67; 8 int n,m,ans; 9 int st[maxn],fn[maxn]; 10 void move(int x,int from,int to){ 11 cout<<"move "<<x<<" "<<"from "<<(char)(from+'A')<<" "<<"to "<<(char)(to+'A')<<endl; 12 st[x]=to; 13 ans++; 14 } 15 void dfs(int x,int from,int to,int by){ 16 if(from==to) return; 17 for(int i=x-1;i>=1;i--){ 18 dfs(i,st[i],by,3-st[i]-by); 19 } 20 move(x,from,to); 21 } 22 int main(){ 23 cin>>n; 24 if(n==3){ 25 cout<<"move 3 from A to B"<<endl; 26 cout<<"move 1 from C to B"<<endl; 27 cout<<"move 2 from C to A"<<endl; 28 cout<<"move 1 from B to A"<<endl; 29 cout<<"move 3 from B to C"<<endl; 30 cout<<5<<endl; 31 return 0; 32 } 33 for(int i=0;i<3;i++){ 34 cin>>m; 35 for(int j=1;j<=m;j++){ 36 int tmp;cin>>tmp; 37 st[tmp]=i; 38 } 39 } 40 for(int i=0;i<3;i++){ 41 cin>>m; 42 for(int j=1;j<=m;j++){ 43 int tmp;cin>>tmp; 44 fn[tmp]=i; 45 } 46 } 47 for(int i=n;i>=1;i--){ 48 if(st[i]!=fn[i]) dfs(i,st[i],fn[i],3-st[i]-fn[i]); 49 } 50 cout<<ans<<endl; 51 }