Cod sursa(job #312098)

Utilizator cvicentiuCiorbaru Vicentiu Marian cvicentiu Data 5 mai 2009 00:08:42
Problema Perle Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include <stdio.h>
#define MaxL 10101
int n, length, v[MaxL];
int a(int poz);
int b(int poz, int FromC);
int c(int poz);

int a(int poz){
	if ( poz <= length ) return 1;
	return 0;
}

int b(int poz, int FromC){
	bool val = false;
	if (FromC == 0){
		switch ( v[poz] ) {
			case 2 : if ( (poz <= (length - 1) ) && ( b(poz + 1, 0) ) ) val = true;
			case 1 : if ( ( poz <= length - 4) && (( v[poz + 2] == 3 ) && ( a(poz + 1) ) && ( a (poz + 3) ) && ( c(poz + 4) ) ) ) val = true;
		};
	}
	if (FromC) {
		switch ( v[poz] ) {
			case 2 : if ( (poz <= (length - 1) ) && ( b(poz + 1, 1) ) ) val = true;
			case 1 : if ( ( poz <= length - 4) && (( v[poz + 2] == 3 ) && ( a(poz + 1) ) && ( a (poz + 3) ) && ( c(poz + 4) ) && ( c(poz + 5) ) ) ) val = true;
		};
	}
	if (val) return 1;
	return 0;
};

int c(int poz){
	bool val = false;
	if ( (poz == length) && (v[poz] == 2) ) val = true;
	if ( (poz == length - 2) && ( (v[poz] == 1) && (v[poz+1] == 2) ) ) val = true; 
	if ( (poz < length - 7) && (v[poz] == 3 &&  b(poz + 1, 1) ) ) val = true;
	if (val) return 1;
	return 0;

}


int test (){
	if ( a(1) && length == 1) return 1;
	if ( b(1,0) ) return 1;
	if ( c(1) ) return 1;
	return 0;
}

int main(){
	
	freopen("perle.in", "r", stdin);
	freopen("perle.out", "w", stdout);
	scanf("%d", &n);
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= 10; j++)
			v[ j ] = 0; 
		scanf("%d", &length);
		for (int j = 1; j <= length; j++)
			scanf("%d", &v[ j ]);
		if ( test() ) 
			printf("1\n");
		else	
			printf("0\n");
	};
		
	

return 0;
}