Cod sursa(job #353921)

Utilizator proflaurianPanaete Adrian proflaurian Data 6 octombrie 2009 19:02:29
Problema Perle Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include<stdio.h>
#include<string.h>
char y[10001],*Y;
int t,i,L,L1,L2,ok,x[10001],p,k;
int prel(),caz1B(),caz1C(),caz2B(),caz2C(),caz3B(),caz3C();
int main()
{ 
	freopen("perle.in","r",stdin);
	freopen("perle.out","w",stdout);
	scanf("%d",&t);
	for(;t;t--)
	{
		scanf("%d",&L);
		for(i=1;i<=L;i++)scanf("%d",&x[i]);
		if(L==1) ok=2;
		else ok=1;
		L1=L;
		L2=1;
		p=1;Y=y;
		if(x[1]==1)*Y=L>=4?'B':'C';
		if(x[1]==2)*Y='B';
		if(x[1]==3)*Y='C';
		while(ok==1)ok=prel();
		printf("%d\n",ok>>1);
	}
  return 0;
}
int prel()
{
    if(x[p]==1){ return *Y=='B'?caz1B():caz1C();}
    if(x[p]==2){ return *Y=='B'?caz2B():caz2C();}
    return *Y=='B'?0:caz3C();
}
int caz1B()
{
    p+=2;L1-=2;
	if(L1<=0||x[p]!=3) return 0;
    p+=2;L1-=2;
	if(L1<=0)return 0;
	*Y='C';
	return 1;
}
int caz1C()
{
    p++;L1--;
	if(L1<=0||x[p]!=2)return 0;
    p+=2;L1-=2;
	if(L1<0)return 0;
    L2--;
    if(L2==0)
	{
		return L1==0?2:0;
	}
    if(L1==0) return 0;
    strcpy(y,y+1);return 1;
}
int caz2B()
{
    p++;L1--;return L1<=0?0:1;
}
int caz2C()
{
    p++;L1++;L2--;
    if(L2<0) return L1==0?2:0;
    if(L1<=0) return 0;
    strcpy(y,y+1);
	return 1;
}
int caz3C()
{
    p++;L1--;L2++;
    if(L1<L2+1) return 0;
    strcpy(y,y+1);
	y[1]='B';
	return 1;
}