Cod sursa(job #2205803)

Utilizator Robert_VRVRobert Vadastreanu Robert_VRV Data 20 mai 2018 12:49:04
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <stdio.h>

int x[10005];

int advanceC(const int& n, const int& pos);

int advanceB(const int& n, const int& pos) {
  if (pos > n)
    return 0;
  if (x[pos] == 2)
    return advanceB(n, pos + 1);
  if (x[pos] == 1 && x[pos + 2] == 3)
    return advanceC(n, pos + 4);
  return 0;
}
int advanceC(const int& n, const int& pos) {
  if (pos > n)
    return 0;
  if (x[pos] == 2)
    return pos + 1;
  if (x[pos] == 3)
    return advanceC(n, advanceB(n, pos + 1));
  if (pos + 1 <= n && x[pos] == 1 && x[pos + 1] == 2)
    return pos + 3;
  return 0;
}

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

  int Q;
  scanf("%d", &Q);
  while(Q--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
      scanf("%d", &x[i]);
    if (n == 1 || advanceB(n, 1) == n + 1 || advanceC(n, 1) == n + 1)
      printf("1\n");
    else
      printf("0\n");
  }

  fclose(stdin);
  fclose(stdout);

  return 0;
}