Cod sursa(job #987539)

Utilizator smaraldaSmaranda Dinu smaralda Data 20 august 2013 23:34:26
Problema Perle Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 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(s[p + 2] != 3)
            return 0;
        if(p + 4 > n)
            return 0;
        p+=4;
        return C();
        }
}

bool C() {
    if(s[p] == 2) {
        if(p == n)
            return 1;
        return 0;
        }
    if(s[p] == 1) {
        if(s[p + 1] != 2)
            return 0;
        if(p + 2 == n)
            return 1;
        return 0;
        }
    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();
            if(!res) {
                p = 1;
                res =  C();
                }
            printf("%d\n",res);
            }
        tc--;
        }
    return 0;
}