Cod sursa(job #2210217)

Utilizator aurelionutAurel Popa aurelionut Data 5 iunie 2018 21:44:55
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <fstream>
#include <stack>
#define A 4
#define B 5
#define C 6

using namespace std;

const int NMAX = 1e4 + 10;
int n, t;
int v[NMAX];
stack <int> st;

bool Solve(int x)
{
	while (!st.empty())
		st.pop();
	st.push(x);
	for (int i = 1;i <= n;++i)
	{
		if (st.empty())
			return false;
		else if (st.top() < 4)
		{
			if (v[i] != st.top())
				return false;
			else
				st.pop();
		}
		else if (st.top() == A)
		{
			st.pop();
		}
		else if (st.top() == B)
		{
			if (v[i] == 1)
			{
				st.pop();
				st.push(C);
				st.push(A);
				st.push(3);
				st.push(A);
			}
			else if (v[i] == 2)
				continue;
			else
				return false;
		}
		else
		{
			if (v[i] == 1)
			{
				st.pop();
				st.push(A);
				st.push(2);
			}
			else if (v[i] == 2)
			{
				st.pop();	
			}
			else
			{
				st.pop();
				st.push(C);
				st.push(B);
			}
		}
	}
	return st.empty();
}

int main()
{
	ifstream fin("perle.in");
	ofstream fout("perle.out");
	fin >> t;
	for (int i = 1;i <= t;++i)
	{
		fin >> n;
		for (int i = 1;i <= n;++i)
			fin >> v[i];
		fout << (Solve(4) || Solve(5) || Solve(6)) << "\n";
	}
	fin.close();
	fout.close();
	return 0;
}