Advertisement
Guest User

Untitled

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