Cod sursa(job #1875042)

Utilizator andru47Stefanescu Andru andru47 Data 10 februarie 2017 18:03:27
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 10000 + 5;
int Q,len,a[NMAX];
inline int c(int pos);
inline int b(int pos)
{
    if (a[pos] == 2)
        return b(pos + 1);
    if (a[pos] == 1 && a[pos + 2] == 3)
        return c(pos + 4);
    return 0;
}
inline int c(int pos)
{
    if (a[pos] == 2)
        return pos + 1;
    else if (a[pos] == 1 && a[pos + 1] == 2 )
        return pos + 3;
    else if (a[pos] == 3)
        return c(b(pos + 1));
    return 0;
}
int main()
{
    freopen("perle.in", "r" ,stdin);
    freopen("perle.out", "w" ,stdout);

    for (scanf("%d\n", &Q); Q; --Q)
    {
        scanf("%d ", &len);
        for (int i = 1; i<=len; ++i)
            scanf("%d", &a[i]);
        int ok = b(1);
        int okk = c(1);
        if (ok == len + 1)ok = 1;
        else ok = 0;
        if (okk == len + 1)okk = 1;
        else okk = 0;
        ok |= okk;
        if (len == 1)
            printf("1\n");
        else
            printf("%d\n", ok);
    }

    return 0;
}