Cod sursa(job #3242083)

Utilizator AndreiNicolaescuEric Paturan AndreiNicolaescu Data 8 septembrie 2024 16:15:58
Problema Perle Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <fstream>

using namespace std;
ifstream cin("perle.in");
ofstream cout("perle.out");
const int Nmax = 10001;
int q, n, v[Nmax], p;
int A();
int B();
int C();
int A()
{
    if(p <= n && (v[p] == 1 || v[p] == 2 || v[p] == 3))
    {
        p++;
        return 1;
    }
    return 0;
}
int B()
{
    if(p > n)
        return 0;

    if(v[p] == 2)
    {
        p++;
        return B();
    }
    else
        if(v[p] == 1)
        {
            p++;
            if(A() == 0)
                return 0;
            if(v[p] != 3)
                return 0;
            p++;
            if(A() == 0)
                return 0;
            return C();

        }
}
int C()
{
    if(p > n)
        return 0;
    if(v[p] == 2)
    {
        p++;
        return 1;
    }
    else
        if(v[p] == 3)
        {
            p++;
            if(B() == 0)
                return 0;
            return C();
        }
        else
            if(p <= n - 2 && v[p] == 1 && v[p+1] == 2)
            {
                p+=2;
                return A();
            }
}
int main()
{
    cin >> q;
    while(q--)
    {
        cin >> n;
        bool ok = 0;
        for(int i=1; i<=n; i++)
            cin >> v[i];
        p = 1;
        if(A() == 1 && p == n + 1)
            ok = 1;
        p = 1;
        if(B() == 1 && p == n + 1)
            ok = 1;
        p = 1;
        if(C() == 1 && p == n + 1)
            ok = 1;

        cout << ok << '\n';
    }
    return 0;
}