Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string.h>
- #include<cstdlib>
- #include<ctime>
- #include <vector>
- #include <algorithm>
- #include <iomanip>
- #include <queue>
- #include <map>
- #include <stack>
- using namespace std;
- int main()
- {
- bool chek=true,chek2=false;
- int N,buff,buff2,top;
- cin>>N;
- stack<int> *n=new stack<int>[N+1];
- stack<int> s;
- for (int i=1;i<=N;i++)
- {
- cin>>buff;
- for (int j=0;j<buff;j++)
- {
- cin>>buff2;
- if (N==2) //костыль
- {
- if (!n[1].empty() && n[1].top()==2 && buff2==1)
- {
- chek2=true;
- }
- if (!n[2].empty() && n[2].top()==1 && buff2==2)
- {
- chek2=true;
- }
- }
- if (buff2!=i) //проверка случая когда все сразу на месте
- {chek=false;}
- n[i].push(buff2);
- }
- }
- if (N==1 || chek) {return 0;}
- if (N==2)
- {
- if (chek2) {cout<<0;return 0;}
- if (!(!n[1].empty() && n[1].top()==2 || !n[2].empty() && n[2].top()==1))
- {
- cout<<0;
- return 0;
- }
- while(!n[1].empty() && n[1].top()==2)
- {
- if (!n[2].empty() && n[2].top()==1)
- {
- cout<<0;
- return 0;
- }
- n[2].push(n[1].top());
- n[1].pop();
- cout<<1<<" "<<2<<endl;;
- }
- while(!n[2].empty() && n[2].top()==1 )
- {
- if (!n[1].empty() && n[1].top()==2)
- {
- cout<<0;
- return 0;
- }
- n[1].push(n[2].top());
- n[2].pop();
- cout<<2<<" "<<1<<endl;;
- }
- return 0;
- }
- for (int i=1;i<=N-1;i++) //перекидываем в последнюю стопку
- while (!n[i].empty())
- {
- n[N].push(n[i].top());
- n[i].pop();
- cout<<i<<" "<<N<<endl;;
- }
- while (!n[N].empty()) //раскладываем по стопкам , в 1 стопку N и 1 ящики
- {
- top=n[N].top();
- if (top==N)
- {
- n[1].push(top);
- cout<<N<<" "<<1<<endl;;
- }
- else
- {
- n[top].push(top);
- cout<<N<<" "<<top<<endl;;
- }
- n[N].pop();
- }
- while (!n[1].empty()) // раскладываем ящики N , первые во 2 стопку
- {
- top=n[1].top();
- if (top==N)
- {
- n[N].push(top);
- cout<<1<<" "<<N<<endl;;
- }
- else
- {
- n[2].push(top);
- cout<<1<<" "<<2<<endl;;
- }
- n[1].pop();
- }
- while (!n[2].empty() && n[2].top()==1) //ящики 1 в 1 стопку
- {
- n[1].push(1);
- n[2].pop();
- cout<<2<<" "<<1<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement