Cod sursa(job #3196138)

Utilizator Cazacu2006RazvanRazvan Cazacu Cazacu2006Razvan Data 22 ianuarie 2024 20:59:55
Problema Perle Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <fstream>

using namespace std;
ifstream fin("perle.in");
ofstream fout("perle.out");
int t,n,x[100001],i,ok;
void B();
void C();
/**
A -> 1 | 2 | 3
B -> 2B | 1A3AC
C -> 2 | 3BC | 12A
*/
void solve()
{
    i=1;
    ok=0;
    if(n==1){
        fout<<1<<"\n";
        return;
    }

    if(x[i]==2)
        B();
    else if(x[i]==3)
        C();
    else if(x[i]=='1' && n==3)
        C();
    else if(x[i]=='1')
        B();
    else
    {

        fout<<"0"<<"\n";
        return;
    }

    fout<<1-ok<<"\n";



}
void B()
{

    if(x[i+1]==2)
    {
        i++;
        if(i==n)
        {
            ok=1;
            return;
        }
        B();



    }
    else if(x[i+1]==1)
    {
        i++;
        if(i==n)
        {
            ok=1;
            return;
        }
        i++;
        if(x[i+1]!=3)
        {
            ok=1;
            return;
        }
        else
            i++;
        if(i==n)
        {
            ok=1;
            return;
        }
        i++;
         if(i==n)
        {
            ok=1;
            return;
        }
        C();
    }
    else
    {
        ok=1;
        return;
    }
}
void C()
{
    if(i+1==n)
    {

        i++;
        return;
    }
    if(x[i+1]==3)
    {
        i++;
        if(i==n)
        {
            ok=1;
            return;
        }
        B();
        if(i==n)
        {
            ok=1;
            return;
        }
        C();
    }
    else if(x[i+1]==1 && x[i+2]==2 && i+3==n)
    {
       i+=3;
       return;
    }
    else
    {
        ok=1;
        return;
    }
}
int main()
{
    fin>>t;
    for(int yp=1;yp<=t;yp++)
    {
        fin>>n;
        for(int p=1;p<=n;p++)
            fin>>x[p];
        solve();
    }

    return 0;
}