Cod sursa(job #20359)

Utilizator tudalexTudorica Constantin Alexandru tudalex Data 21 februarie 2007 12:04:28
Problema Perle Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <stdio.h>

int a[10001],n,i,t,max;
int presumec(int x,int lvl);
int presumeb(int x,int lvl)
{
	//if (x>max) max=x;
    if (x>n) return 0;
	if (a[x]==2) return presumeb(x+1,lvl+1);
	if (a[x]==1 && a[x+2]==3 && x+4<=n) return presumec(x+4,lvl+1);
	return 0;
}

int presumec(int x,int lvl)
{
	int r;
	//if (x>max) max=x;
	if (x>n) return 0;
	if (a[x]==1 && a[x+1]==2 && x+1<=n) return x+3;
	if (a[x]==3)
	{
		r=presumeb(x+1,lvl+1);
		if (r!=0) return presumec(r+1,lvl+1); else return 0;
	} 
	if (a[x]==2 && lvl>1) return x+1;
	if (lvl==1 && a[x]==2 && n==1) return x+1;
	return 0;
}

int main()
{
	freopen("perle.in","r",stdin);
	freopen("perle.out","w",stdout);
	scanf("%d",&t);
	for (;t>0;--t)
	{
		scanf("%d",&n);
		for (i=1;i<=n;++i) scanf("%d",&a[i]);
		
		if (n==1) printf("1\n"); else
		//max=0;
        if (presumeb(1,1)!=0) printf("1\n"); else
        
          if (presumec(1,1)!=0) printf("1\n"); 
              
           
        else		
		printf("0\n");
	}
	return 0;
}