Cod sursa(job #189647)

Utilizator alex_mircescuAlex Mircescu alex_mircescu Data 16 mai 2008 17:17:58
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <stdio.h>
#include <math.h>

long n, l, a[10001];

long test_b(int i);
long test_c(int i);

long test_b(int i) {
	for (; i < l; ++i) {
		if (a[i] != 2) {
			break;
		}
	}
	if (i == l) {
		return 0;
	}
	if (l < i + 5) {
		return 0;
	}
	if (a[i] != 1 || a[i + 2] != 3) {
		return 0;
	}
	return test_c(i + 4);
}

long test_c(int i) {
	if (a[i] == 2) {
		return i + 1;
	}
	if (a[i] == 1) {
		if (l < i + 3) {
			return 0;
		}
		if (a[i + 1] == 2) {
			return i + 3;
		}
	}

	i = test_b(i + 1);
	if (i == l || i == 0) {
		return 0;
	}
	return test_c(i);
}


long test() {
	if (l == 1) {
		return 1;
	}

	if (a[0] == 1) {
		if (l == 3 && a[1] == 2) {
			return 1;
		}
		if (l >= 3 && a[2] == 3) {
			return (test_c(4) == l);
		}
	}
	if (a[0] == 2) {
		return (test_b(0) == l);
	}

	return (test_c(0) == l);
}

int main()
{
	int i,j;
	freopen("perle.in","r",stdin);
	freopen("perle.out","w",stdout);
	scanf("%ld", &n);
	for (i = 0;i < n; ++i) {
		scanf("%ld", &l);
		for (j = 0; j < l; ++j) {
			scanf("%ld", &a[j]);
		}
		printf("%ld\n", test());
	}
	return 0;
}