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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include <iostream> #include <vector> #include <algorithm> using namespace std; const int maxn=50; int n,n1,a1,now[maxn],ob[maxn]; vector<int> hanoi[5],cache; long long ans; char ForOut[4]={'0','A','B','C'};
inline void out(int st,int ed,int x){ ans+=1; cout<<"move "<<x<<" from "<<ForOut[st]<<" to "<<ForOut[ed]<<endl; } inline bool check(int x){ for(int i=1;i<=3;i++) if(!hanoi[i].empty() && x==hanoi[i].back() ) return true; return false; } inline void upd(int x,int c){ hanoi[now[x]].pop_back(); hanoi[c].push_back(x); swap(now[x],c); } void solve(int a,int b,int c,int x){ if(check(x)){ if(!hanoi[c].empty() && x>hanoi[c].back()){ solve(c,a,b,hanoi[c].back()); } out(now[x],c,x); upd(x,c); return; } for(int i=x-1;i>=1;i--){ if(now[i]==b) continue; solve(now[i],6-now[i]-b,b,i); } out(a,c,x); upd(x,c); } bool cmp(int a,int b){ if(a!=b) return a>b; } int main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n; for(int i=1;i<=3;i++){ cin>>n1; for(int _=1;_<=n1;_++){ cin>>a1; now[a1]=i; cache.push_back(a1); } sort(cache.begin(),cache.end(),cmp); for(int j=0;j<cache.size();j++) hanoi[i].push_back(cache[j]); cache.clear(); } for(int i=1;i<=3;i++){ cin>>n1; for(int _=1;_<=n1;_++){ cin>>a1; ob[a1]=i; } } for(int i=n;i>=1;i--){ if(now[i]!=ob[i]) solve(now[i],6-now[i]-ob[i],ob[i],i); } cout<<ans<<endl; cin>>n; }
|