Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <queue>
- #include <string.h>
- using namespace std;
- typedef struct pilha{
- int t1, t2, t3;
- pilha(int a = 0, int b = 0, int c = 0) : t1(a), t2(b), t3(c) {}
- } pi;
- int main()
- {
- int p1[101], p2[101], p3[101], N, s;
- queue<pi> q;
- while(scanf("%d", &N) && N)
- {
- s = 0;
- for(int i = 0; i < N; i++){
- scanf("%d %d %d", &p1[i], &p2[i], &p3[i]);
- s+=p1[i]+p2[i]+p3[i];
- }
- while(!q.empty()) q.pop();
- int sucess = 0;
- q.push(pilha(0, 0, 0));
- while(!q.empty())
- {
- pilha f = q.front();
- if(f.t1 == N && f.t2 == N && f.t3 == N){
- sucess = 1;
- break;
- }
- if(f.t1 < N)
- if(p1[f.t1] % 3 == 0) { //t1
- q.push(pilha(f.t1+1, f.t2, f.t3));
- }
- if(f.t1 < N && f.t2 < N)
- if((p1[f.t1] + p2[f.t2]) % 3 == 0) { //t1 t2
- q.push(pilha(f.t1+1, f.t2+1, f.t3));
- }
- if(f.t1 < N && f.t2 < N && f.t3 < N)
- if((p1[f.t1] + p2[f.t2] + p3[f.t3]) % 3 == 0) { //t1 t2 t3
- q.push(pilha(f.t1+1, f.t2+1, f.t3+1));
- }
- if(f.t1 < N && f.t3 < N)
- if((p1[f.t1] + p3[f.t3]) % 3 == 0) { //t1 t3
- q.push(pilha(f.t1+1, f.t2, f.t3+1));
- }
- if(f.t2 < N)
- if(p2[f.t2] % 3 == 0) { //t2
- q.push(pilha(f.t1, f.t2+1, f.t3));
- }
- if(f.t2 < N && f.t3 < N)
- if((p2[f.t2] + p3[f.t3]) % 3 == 0){ //t2 t3
- q.push(pilha(f.t1, f.t2+1, f.t3+1));
- }
- if(f.t3 < N)
- if(p3[f.t3] % 3 == 0) { //t3
- q.push(pilha(f.t1, f.t2, f.t3+1));
- }
- q.pop();
- }
- printf("%d\n", sucess);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement