Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <queue>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. typedef struct pilha{
  8.     int t1, t2, t3;
  9.     pilha(int a = 0, int b = 0, int c = 0) : t1(a), t2(b), t3(c) {}
  10. } pi;
  11.  
  12.  
  13. int main()
  14. {
  15.     int p1[101], p2[101], p3[101], N, s;
  16.     queue<pi> q;
  17.  
  18.     while(scanf("%d", &N) && N)
  19.     {
  20.         s = 0;
  21.  
  22.         for(int i = 0; i < N; i++){
  23.             scanf("%d %d %d", &p1[i], &p2[i], &p3[i]);
  24.             s+=p1[i]+p2[i]+p3[i];
  25.         }
  26.  
  27.         while(!q.empty()) q.pop();
  28.  
  29.         int sucess = 0;
  30.         q.push(pilha(0, 0, 0));
  31.  
  32.         while(!q.empty())
  33.         {
  34.             pilha f = q.front();
  35.  
  36.             if(f.t1 == N && f.t2 == N && f.t3 == N){
  37.                 sucess = 1;
  38.                 break;
  39.             }
  40.  
  41.             if(f.t1 < N)
  42.             if(p1[f.t1] % 3 == 0) { //t1
  43.                 q.push(pilha(f.t1+1, f.t2, f.t3));
  44.             }
  45.  
  46.             if(f.t1 < N && f.t2 < N)
  47.             if((p1[f.t1] + p2[f.t2]) % 3 == 0) { //t1 t2
  48.                 q.push(pilha(f.t1+1, f.t2+1, f.t3));
  49.             }  
  50.  
  51.             if(f.t1 < N && f.t2 < N && f.t3 < N)
  52.             if((p1[f.t1] + p2[f.t2] + p3[f.t3]) % 3 == 0) { //t1 t2 t3
  53.                 q.push(pilha(f.t1+1, f.t2+1, f.t3+1));
  54.             }
  55.  
  56.             if(f.t1 < N && f.t3 < N)
  57.             if((p1[f.t1] + p3[f.t3]) % 3 == 0) { //t1 t3
  58.                 q.push(pilha(f.t1+1, f.t2, f.t3+1));
  59.             }
  60.  
  61.             if(f.t2 < N)
  62.             if(p2[f.t2] % 3 == 0) { //t2
  63.                 q.push(pilha(f.t1, f.t2+1, f.t3));
  64.             }
  65.  
  66.             if(f.t2 < N && f.t3 < N)
  67.             if((p2[f.t2] + p3[f.t3]) % 3 == 0){ //t2 t3
  68.                 q.push(pilha(f.t1, f.t2+1, f.t3+1));
  69.             }
  70.                            
  71.             if(f.t3 < N)
  72.             if(p3[f.t3] % 3 == 0) { //t3
  73.                 q.push(pilha(f.t1, f.t2, f.t3+1));
  74.             }
  75.  
  76.             q.pop();
  77.         }
  78.  
  79.         printf("%d\n", sucess);
  80.  
  81.     }
  82.  
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement