Cod sursa(job #2272521)

Utilizator MiricaMateiMirica Matei MiricaMatei Data 30 octombrie 2018 11:29:00
Problema Perle Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 10005;

int n;
int v[MAXN];

int A(int poz);
int B(int poz);
int C(int poz);

int C(int poz) {
  if (v[poz] == 2)
    return poz + 1;
  if (poz >= n)
    return 0;
  if (v[poz] == 3)
    return C(B(poz + 1));
  if (poz + 2 <= n && v[poz] == 1 && v[poz + 1] == 2)
    return A(poz + 2);
  return 0;
}

int B(int poz) {
  if (poz >= n)
    return 0;
  if (v[poz] == 2)
    return B(poz + 1);
  if (poz + 4 <= n && v[poz] == 1 && v[poz + 2] == 3)
    return C(poz + 4);
  return 0;
}

int A(int poz) {
  if (poz == n)
    return n + 1;
  return 0;
}

int main() {
  freopen("perle.in", "r", stdin);
  freopen("perle.out", "w", stdout);

  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
      scanf("%d", &v[i]);
    printf("%d\n", (A(1) == n + 1) | (B(1) == n + 1) | (C(1) == n + 1));
  }

  return 0;
}