Cod sursa(job #492153)

Utilizator S7012MYPetru Trimbitas S7012MY Data 13 octombrie 2010 16:34:33
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
/*
 * File:   main.cpp
 * Author: petru
 *
 * Created on 2010-10-13
 */


#include <cstdio>
#define deb(n) fprintf(stderr,"%d ",(n));
#define DN 10005

int n,p[DN],t;

int t1(int),t2(int);

int t1(int poz) {
    if (poz>n) return 0;
    if (2==p[poz] && n>poz) return t1(poz+1);
    if (1==p[poz] && 3==p[poz+2] && n>poz+3) return t2(poz+4);
    return 0;
}

int t2(int poz) {
    if (poz>n) return 0;
    if (2==p[poz] && n>=poz) return poz+1;
    if (1==p[poz] && 2==p[poz+1] && n>=poz+2) return poz+3;
    if (3==p[poz]) {
        int x=t1(poz+1);
        if (x) return t2(x);
    }
    return 0;
}

bool check() {
    return (t1(1)>n) || (t2(1)>n);
}

int main()
{
	freopen("perle.in","r",stdin);
	freopen("perle.out","w",stdout);
	for(scanf("%d",&t);t;--t) {
	    scanf("%d",&n);
	    if(1==n) {
	        printf("1\n");
	        scanf("%d",&n);
	        continue;
	    }
	    for(int i=1; i<=n; ++i) scanf("%d",&p[i]);
	    printf("%d\n",check());
	}
	return 0;
}