Cod sursa(job #2205801)

Utilizator Robert_VRVRobert Vadastreanu Robert_VRV Data 20 mai 2018 12:47:15
Problema Perle Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <stdio.h>

int advanceC(const int& n, const int& pos, int x[]);

int advanceB(const int& n, const int& pos, int x[]) {
  if (pos > n)
    return 0;
  if (x[pos] == 2)
    return advanceB(n, pos + 1, x);
  if (x[pos] == 1 && x[pos + 2] == 3)
    return advanceC(n, pos + 4, x);
  return 0;
}
int advanceC(const int& n, const int& pos, int x[]) {
  if (pos > n)
    return pos;
  if (x[pos] == 2)
    return pos + 1;
  if (x[pos] == 3)
    return advanceC(n, advanceB(n, pos + 1, x), x);
  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);
    int x[1 + n];
    for (int i = 1; i <= n; i++)
      scanf("%d", &x[i]);
    if (n == 1 || advanceB(n, 1, x) == n + 1 || advanceC(n, 1, x) == n + 1)
      printf("1\n");
    else
      printf("0\n");
  }

  fclose(stdin);
  fclose(stdout);

  return 0;
}