Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <queue>
- #include <string.h>
- #define MAX 105
- 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[MAX], p2[MAX], p3[MAX], N;
- queue<pi> q;
- int v[MAX][MAX][MAX];
- while(scanf("%d", &N) && N)
- {
- for(int i = 0; i < N; i++)
- scanf("%d %d %d", &p1[N-i], &p2[N-i], &p3[N-i]);
- while(!q.empty()) q.pop();
- memset(v, 0, sizeof v);
- int sucess = 0;
- q.push(pilha(N, N, N));
- while(!q.empty())
- {
- pilha f = q.front();
- if(f.t1 == 0 && f.t2 == 0 && f.t3 == 0){
- sucess = 1;
- break;
- }
- v[f.t1][f.t2][f.t3] = 1;
- if(f.t1 > 0) {
- if(p1[f.t1] % 3 == 0) { //t1
- if(!v[f.t1-1][f.t2][f.t3])
- q.push(pilha(f.t1-1, f.t2, f.t3));
- }
- }
- if(f.t1 > 0 && f.t2 > 0) {
- if((p1[f.t1] + p2[f.t2]) % 3 == 0) { //t1 t2
- if(!v[f.t1-1][f.t2-1][f.t3]) {
- q.push(pilha(f.t1-1, f.t2-1, f.t3));
- }
- }
- }
- if(f.t1 > 0 && f.t2 > 0 && f.t3 > 0) {
- if((p1[f.t1] + p2[f.t2] + p3[f.t3]) % 3 == 0) { //t1 t2 t3
- if(!v[f.t1-1][f.t2-1][f.t3-1]) {
- q.push(pilha(f.t1-1, f.t2-1, f.t3-1));
- }
- }
- }
- if(f.t1 > 0 && f.t3 > 0) {
- if((p1[f.t1] + p3[f.t3]) % 3 == 0) { //t1 t3
- if(!v[f.t1-1][f.t2][f.t3-1]) {
- q.push(pilha(f.t1-1, f.t2, f.t3-1));
- }
- }
- }
- if(f.t2 > 0) {
- if(p2[f.t2] % 3 == 0) { //t2
- if(!v[f.t1][f.t2-1][f.t3]) {
- q.push(pilha(f.t1, f.t2-1, f.t3));
- }
- }
- }
- if(f.t2 > 0 && f.t3 > 0) {
- if((p2[f.t2] + p3[f.t3]) % 3 == 0){ //t2 t3
- if(!v[f.t1][f.t2-1][f.t3-1]) {
- q.push(pilha(f.t1, f.t2-1, f.t3-1));
- }
- }
- }
- if(f.t3 > 0) {
- if(p3[f.t3] % 3 == 0) { //t3
- if(!v[f.t1][f.t2][f.t3-1]) {
- 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