Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int n;
- vector<int>a;
- vector<vector<int>>ans;
- void moveall(int k,int src,int dest){
- if(k<=0)return;
- int aux=6-src-dest;
- if(a[k-1]==0){
- // move up k-1 to aux, then move kth to dest, then move k-1 from aux to dest
- moveall(k-1,src,aux);
- ans.push_back({k,src,dest});
- moveall(k-1,aux,dest);
- }else{
- // keep a[k-1]+1 lowest rings to source, move k-a[k-1]-1 to aux
- // then move kth to dest
- // then move the k-a[k-1]-1 from aux to source back
- //then call the recursion again for (k-1)th
- moveall(k-a[k-1]-1,src,aux);
- ans.push_back({k,src,dest});
- moveall(k-a[k-1]-1,aux,src);
- moveall(k-1,src,dest);
- }
- }
- void solve() {
- cin>>n;
- a.resize(n);
- bool ok=true;
- for(int i=0;i<n;i++){
- cin>>a[i];
- }
- for(int i=0;i<n;i++){
- if(a[i]>i){
- ok=false;break;
- }
- }
- if(!ok){
- cout<<"No"<<endl;return;
- }
- cout<<"Yes"<<endl;
- //s,a,d
- //f(n,s,d)
- moveall(n,1,3);
- cout<<ans.size()<<endl;
- for(auto&vec:ans){
- cout<<vec[0]<<" "<<vec[1]<<" "<<vec[2]<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment