Pagini recente » Cod sursa (job #418827) | Cod sursa (job #1057387) | Cod sursa (job #2253334) | Cod sursa (job #3252391) | Cod sursa (job #1522837)
#include <cstdio>
#include <iostream>
#include <set>
#include <climits>
#include <map>
#include <algorithm>
#include <list>
#include <vector>
#include <utility>
using namespace std;
int v[10005], n;
bool f(int poz, char c, int nc) {
// cout << poz << " " << c << "\n";
if (c == 'A') {
if (poz == n) {
return true;
}
return false;
}
if (c == 'B') {
if (v[poz] == 1) {
if (poz + 4 <= n && v[poz + 2] == 3) {
return f(poz + 4, 'C', nc);
}
return false;
}
if (v[poz] == 2) {
if (poz + 1 <= n) {
return f(poz + 1, 'B', nc);
}
return false;
}
return false;
}
if (v[poz] == 1) {
if (poz + 2 <= n) {
if (v[poz + 1] == 2) {
if (poz + 2 == n) {
if (nc == 0) {
return true;
}
return false;
}
if (nc == 0) {
return false;
}
return f(poz + 3, 'C', nc - 1);
}
return false;
}
return false;
}
if (v[poz] == 2) {
if (poz == n) {
if (nc == 0) {
return true;
}
return false;
}
if (nc == 0) {
return false;
}
return f(poz + 1, 'C', nc - 1);
}
if (poz == n) {
return false;
}
return f(poz + 1, 'B', nc + 1);
}
int main() {
freopen("perle.in", "r", stdin);
freopen("perle.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
for (int j = 1; j <= n; j++) {
cin >> v[j];
}
cout << (f(1, 'A', 0) || f(1, 'B', 0) || f(1, 'C', 0)) << "\n";
//cout << f(1, 'B', 0);
}
return 0;
}