Cod sursa(job #189650)

Utilizator scvalexAlexandru Scvortov scvalex Data 16 mai 2008 17:35:45
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <iostream>
#include <fstream>

using namespace std;

int L;
int V[10001];
int pos;

bool tryB();
bool tryC();

bool tryA()
{
	if (pos >= L)
		return false;
	++pos;
	return true;
}

bool tryB()
{
	if (pos >= L)
		return false;

	if (V[pos] == 2) {
		++pos;
		return tryB();
	}

	if (V[pos] == 1) {
		++pos;
		if (!tryA())
			return false;
		if (V[pos++] != 3)
			return false;
		if (!tryA())
			return false;
		return tryC();
	}
 
	return false;
}

bool tryC()
{
	if (pos >= L)
		return false;
	
	if (V[pos] == 2) {
		++pos;
		return true;
	}

	if (V[pos] == 3) {
		++pos;
		if (!tryB())
			return false;
		return tryC();
	}

	if (V[pos] == 1) {
		++pos;
		if (V[pos++] != 2)
			return false;
		return tryA();
	}

	return false;
}

bool canMake()
{
	if (L == 1)
		return true;
	if (L == 2)
		return false;
	pos = 0;
	if (tryB() && (pos == L))
		return true;
	pos = 0;
	return (tryC() && (pos == L));
}

int main(int argc, char *argv[])
{
	FILE *fi = fopen("perle.in", "r");
	int N;
	fscanf(fi, "%d", &N);
	ofstream fout("perle.out");
	while (N--) {
		fscanf(fi, "%d", &L);
		for (int i(0); i < L; ++i)
			fscanf(fi, "%d", V+i);
		fout << canMake() << endl;
	}
	fout.close();
	fclose(fi);

	return 0;
}