Cod sursa(job #825987)

Utilizator ericptsStavarache Petru Eric ericpts Data 29 noiembrie 2012 21:13:42
Problema Perle Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <cstdio>
using namespace std;
const int maxn = 10011;
short v[maxn];
int l;
int pos;
bool c();
bool b()
{

    if(v[pos] == 2)
    {
        ++pos;
        return b();
    }
    if(v[pos] == 1 && v[pos+2] == 3)
    {
        pos+=4;
        return c();
    }
    return 0;
}

bool c()
{
    if(l == pos && v[pos] == 2)
        return 1;
    if(pos == l - 2 && v[pos] == 1 && v[pos+1] == 2)
        return 1;
    if(v[pos] == 3)
    {
        ++pos;
        return (b() && c());
    }
    return 0;
}

int main()
{
    int n;
    freopen("perle.in","r",stdin);
    freopen("perle.out","w",stdout);
    setvbuf(stdin,NULL,_IOFBF,1024);
    scanf("%d",&n);
    int i;
    bool ok;
    while(n--)
    {
        scanf("%d",&l);
        for(i=1;i<=l;++i)
            scanf("%d",v+i);
        pos = 1;
        if(l == 1)
            printf("1\n");
        else
        {
            ok = 0;
            if(b())
                ok = 1;
            pos = 1;
            if(c())
                ok = 1;
            printf("%d\n",ok);
        }
    }
    return 0;
}