Cod sursa(job #2272497)

Utilizator MiricaMateiMirica Matei MiricaMatei Data 30 octombrie 2018 10:58:05
Problema Perle Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 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 (poz > n)
    return -1;
  if (v[poz] == 2)
    return poz;
  if (v[poz] == 3)
    return C(B(poz + 1));
  if (v[poz] == 1 && v[poz + 1] == 2)
    return A(poz + 2);
  return -1;
}

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

int A(int poz) {
  if (poz == n)
    return n;
  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]);
    v[n + 1] = 0;
    printf("%d\n", (A(1) == n) | (B(1) == n) | (C(1) == n));
  }

  return 0;
}