Cod sursa(job #1606068)

Utilizator kassay_akosKassay Akos kassay_akos Data 19 februarie 2016 20:02:01
Problema Perle Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <iostream>
#include <fstream>
#include <string.h>
#include <stack>

using namespace std;
const int nmax = 10003;
stack<int > q;
int a[nmax];
int n;

// Let A = 10 B = 11 and C = 12

bool res(int len) {
	int ind = 0, curr;
	bool ended = false;

	while (ind < len && !ended) {
		curr = q.top();
		q.pop();
		switch (curr)
		{
		case 1:
		case 2:
		case 3:
			if (curr != a[ind]) ended = true;
			break;
		case 11:
			if (a[ind] == 2)  
				q.push(11);			// b
			else if (a[ind] == 1) {
				q.push(12);			// c
				q.push(10);			// a
				q.push(3);			// 3
				q.push(10);			// a
			}
			else ended = true;
			break;
		case 12 :
			if (a[ind] == 1) {
				q.push(10);			// a
				q.push(2);			// 2
			}
			else if (a[ind] == 3) {
				q.push(12);			// C
				q.push(11);			// B
			}
			break;
		}
		ind++;
	}

	if (ind == len && ended == false && q.empty())
		return true;
	return false;
}


int main(){
	freopen("perle.in", "r", stdin);
	freopen("perle.out", "w", stdout);
	scanf("%d", &n);

	int len;
	bool ok;
	for (int i = 0; i < n; i++){
		scanf("%d", &len);
		for (int j = 0; j < len; j++)
			scanf("%d", &a[j]);
		a[len] = 7;		// end point
		ok = false;
		
		// test with starting A
		if (len == 1)	
			ok = true;
		else if (a[0] == 2) {
			q.push(11);					// 2B
			ok = res(len);
		}
		else if (a[0] == 3) {
			q.push(12);					// 3BC
			ok = res(len);
		}
		else if (a[0] == 1 && len == 3) {
			if (a[1] == 2) ok = true;
		}
		else {
			q.push(11);				// 1A3AC
			ok = res(len);
		}

		printf("%d\n", ok);
	}

	fclose(stdin);
	fclose(stdout);
	return 0;
}