Cod sursa(job #987640)

Utilizator smaraldaSmaranda Dinu smaralda Data 21 august 2013 10:37:39
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include<stdio.h>
#include<string.h>
int p,n,s[10005];
bool B();
bool C();

bool B() {
    if(s[p] == 3)
        return 0;
    if(s[p] == 2) {
        if(p + 1 > n)
            return 0;
        ++p;
        return B();
        }
    if(s[p] == 1) {
        if(p + 4 > n)
            return 0;

        if(s[p + 2] != 3)
            return 0;
        p+=4;
        return C();
        }
}

bool C() {
    if(s[p] == 2) {
        ++p;
        return 1;
        }
    if(s[p] == 1) {
        if (p + 2 > n)
            return 0;
        if(s[p + 1] != 2)
            return 0;
        p += 3;
        return 1;
        }
    if(s[p] == 3) {
        ++p;
        return B() && C();
        }
}

int main () {
    freopen("perle.in","r",stdin);
    freopen("perle.out","w",stdout);
    int res,tc,i;
    scanf("%d",&tc);
    while(tc) {
        scanf("%d",&n);
        memset(s,0,sizeof(s));
        for(i = 1; i <= n ;i++)
            scanf("%d",&s[i]);
        p = 1;
        if(n==1)
            printf("1\n");
        else    {
            res = B() && (p == n + 1);
            if (!res) {
                p=1;
                res = C() && (p == n + 1);
            }
            printf("%d\n",res);
            }
        tc--;
        }
    return 0;
}