Cod sursa(job #641377)

Utilizator sebii_cSebastian Claici sebii_c Data 28 noiembrie 2011 02:05:22
Problema Perle Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <stdio.h>
#define NMAX 10000

int A[NMAX + 10];
int n;

int B(int); int C(int);

int B(int pos)
{
	if (pos > n + 1)
		return pos;

	if (A[pos] == 2)
		return B(pos + 1);
	
	else if (A[pos] == 1 && A[pos + 2] == 3)
		return C(pos + 4);
	
	return -1;
}

int C(int pos)
{
	if (A[pos] == 2)
		return pos + 1;
	if (A[pos] == 1 && A[pos + 1] == 2)
		return pos + 3;
	if (A[pos] == 3)
		return C(B(pos + 1));

	return -1;
}

int main()
{
	freopen("perle.in", "r", stdin);
	freopen("perle.out", "w", stdout);
	int i, t;
	scanf("%d", &t);
	for ( ; t; --t) {
		scanf("%d", &n);
		for (i = 1; i <= n; ++i)
			scanf("%d", &A[i]);

		if (n == 1) {
			printf("1\n");
			continue;
		}
		int res = B(1);
		if (res != n + 1)
			res = C(1);
		if (res == n + 1)
			printf("1\n");
		else
			printf("0\n");
	}
	return 0;
}