Cod sursa(job #826008)

Utilizator ericptsStavarache Petru Eric ericpts Data 29 noiembrie 2012 21:30:40
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <cstdio>
using namespace std;

const int maxn = 10011;

short v[maxn];
int l;
int b(int poz);
int c(int poz);

int b(int poz)
{
	if(poz>l)
		return -1;
	if(v[poz] == 2)
		return b(poz+1);
	if(v[poz] == 1 && v[poz+2] == 3)
		return c(poz+4);
	return -1;
}

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