Advertisement
SergeyPGUTI

10.1.13

Mar 29th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string.h>
  4. #include<cstdlib>
  5. #include<ctime>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <iomanip>
  9. #include <queue>
  10. #include <map>
  11. #include <stack>
  12.  
  13.  
  14.  
  15. using namespace std;
  16.  
  17.  
  18.  
  19.  
  20.  
  21. int main()
  22. {
  23.     bool chek=true,chek2=false;
  24.     int N,buff,buff2,top;
  25.     cin>>N;
  26.     stack<int> *n=new stack<int>[N+1];
  27.     stack<int> s;
  28.     for (int i=1;i<=N;i++)
  29.     {
  30.  
  31.         cin>>buff;
  32.         for (int j=0;j<buff;j++)
  33.         {
  34.             cin>>buff2;
  35.             if (N==2) //костыль
  36.             {
  37.                 if (!n[1].empty() && n[1].top()==2 && buff2==1)
  38.                 {
  39.                     chek2=true;
  40.                 }
  41.  
  42.                 if (!n[2].empty() && n[2].top()==1 && buff2==2)
  43.                 {
  44.                      chek2=true;
  45.                 }
  46.             }
  47.             if (buff2!=i) //проверка случая когда все сразу на месте
  48.                 {chek=false;}
  49.             n[i].push(buff2);
  50.         }
  51.     }
  52.     if (N==1 || chek) {return 0;}
  53.     if (N==2)
  54.     {
  55.         if (chek2) {cout<<0;return 0;}
  56.         if (!(!n[1].empty() && n[1].top()==2 || !n[2].empty() && n[2].top()==1))
  57.         {
  58.             cout<<0;
  59.             return 0;
  60.         }
  61.         while(!n[1].empty() && n[1].top()==2)
  62.         {
  63.             if (!n[2].empty() && n[2].top()==1)
  64.             {
  65.                 cout<<0;
  66.                 return 0;
  67.             }
  68.             n[2].push(n[1].top());
  69.             n[1].pop();
  70.             cout<<1<<" "<<2<<endl;;
  71.         }
  72.         while(!n[2].empty() && n[2].top()==1 )
  73.         {
  74.             if (!n[1].empty() && n[1].top()==2)
  75.             {
  76.                 cout<<0;
  77.                 return 0;
  78.             }
  79.             n[1].push(n[2].top());
  80.             n[2].pop();
  81.             cout<<2<<" "<<1<<endl;;
  82.         }
  83.         return 0;
  84.     }
  85.  
  86.  
  87.  
  88.  
  89.         for (int i=1;i<=N-1;i++) //перекидываем в последнюю стопку
  90.         while (!n[i].empty())
  91.         {
  92.            n[N].push(n[i].top());
  93.            n[i].pop();
  94.            cout<<i<<" "<<N<<endl;;
  95.         }
  96.         while (!n[N].empty()) //раскладываем по стопкам , в 1 стопку N и 1 ящики
  97.         {
  98.             top=n[N].top();
  99.             if (top==N)
  100.             {
  101.                 n[1].push(top);
  102.                 cout<<N<<" "<<1<<endl;;
  103.             }
  104.             else
  105.             {
  106.                 n[top].push(top);
  107.                 cout<<N<<" "<<top<<endl;;
  108.             }
  109.             n[N].pop();
  110.  
  111.         }
  112.         while (!n[1].empty()) // раскладываем ящики N , первые во 2 стопку
  113.         {
  114.             top=n[1].top();
  115.             if (top==N)
  116.             {
  117.                 n[N].push(top);
  118.                 cout<<1<<" "<<N<<endl;;
  119.             }
  120.             else
  121.             {
  122.                 n[2].push(top);
  123.                 cout<<1<<" "<<2<<endl;;
  124.             }
  125.             n[1].pop();
  126.  
  127.         }
  128.         while (!n[2].empty() && n[2].top()==1) //ящики 1 в 1 стопку
  129.         {
  130.             n[1].push(1);
  131.             n[2].pop();
  132.             cout<<2<<" "<<1<<endl;
  133.         }
  134.  
  135.     return 0;
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement