Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include <map>
- using namespace std;
- typedef pair<int, int> ii;
- int main(){
- int t;
- cin>>t;
- pair<int,int>b;
- for(int k=0;k<t;k++){
- int a;
- cin>>a;
- int r;
- vector<int>code;
- pair<int,int>b;
- for(int i=0;i<4;i++){
- code.push_back(-1);
- }
- int c[10][4];
- vector<int>vip;
- for(int i=0;i<10;i++){
- for(int j=0;j<4;j++){
- c[i][j] = -1;
- }
- }
- vector<pair<int,int> > vp;
- for(int i=0;i<a;i++){
- cin>>r;
- vip.push_back(r);
- cin>>b.first;
- cin>>b.second;
- vp.push_back(b);
- }
- int w1,w2;
- int one,two,three,four;
- int o=0;
- int ae=0;
- while(ae < 1000){
- ae++;
- for(int i=0;i<vip.size();i++){
- o=0;
- b = vp[i];
- w2 = b.second;
- w1 = b.first;
- one=vip[i]/1000;
- two=(vip[i]/100)%10;
- three = (vip[i]/10)%10;
- four=vip[i]%10;
- vector<pair<int,int> >y;
- /*
- for(int t1=0;t1<4;t1++){
- cout<<code[t1];
- }
- cout<<endl;
- */
- if(w1 == 0 && w2 == 0){
- for(int j=0;j<4;j++){
- c[one][j] = 0;
- c[two][j] = 0;
- c[three][j] = 0;
- c[four][j] = 0;
- }
- }
- else if(w1 == 0){
- c[one][0] = 0;
- c[two][1] = 0;
- c[three][2] = 0;
- c[four][3] = 0;
- }
- if(c[one][0] == 0){
- o++;
- }
- else{
- y.push_back(ii(one,0));
- }
- if(c[two][1] == 0){
- o++;
- }
- else{
- y.push_back(ii(two,1));
- }
- if(c[three][2] == 0){
- o++;
- }
- else{
- y.push_back(ii(three,2));
- }
- if(c[four][3] == 0){
- o++;
- }
- else{
- y.push_back(ii(four,3));
- }
- if(4 - o == w1 && o != 0){
- while(!y.empty()){
- b = y.back();
- y.pop_back();
- code[b.second] = b.first;
- for(int j=0;j<10;j++){
- if(i != b.first){
- c[i][b.second] = 0;
- }
- }
- }
- }
- y.clear();
- o=0;
- if(c[one][1] == -1 || c[one][2] == -1 || c[one][3] == -1){
- o++;
- y.push_back(ii(one,0));
- }
- if(c[two][0] == -1 || c[two][2] == -1 || c[two][3] == -1){
- o++;
- y.push_back(ii(two,1));
- }
- if(c[three][1] == -1 || c[three][1] == -1 || c[three][3] == -1){
- o++;
- y.push_back(ii(three,2));
- }
- if(c[four][1] == -1 || c[four][2] == -1 || c[four][0] == -1){
- o++;
- y.push_back(ii(four,3));
- }
- if(o == w2 && o != 0){
- while(!y.empty()){
- b = y.back();
- y.pop_back();
- code.push_back(b.first);
- }
- }
- y.clear();
- o=0;
- int y1=0;
- if(code.size() > 4){
- for(int k=4;k<code.size();k++){
- if(code[i] != -1){
- for(int w=0;w<4;w++){
- if(c[code[i]][w] == -1){
- o++;
- y1 = w;
- }
- }
- if(o == 1){
- c[code[i]][y1] = 1;
- code[y1] = code[i];
- code[i] = -1;
- }
- }
- }
- }
- }
- }
- bool w3=0;
- for(int i=0;i<4;i++){
- if(code[i] == -1){
- w3 = 1;
- }
- }
- if(w3){
- cout<<0<<endl;
- }
- else{
- for(int i=0;i<4;i++){
- cout<<code[i];
- }
- cout<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement