Pagini recente » Cod sursa (job #672655) | Cod sursa (job #2187320) | Cod sursa (job #1532071) | Cod sursa (job #1153796) | Cod sursa (job #1606068)
#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;
}