Cod sursa(job #2947554)

Utilizator TheEpicWipedCreaVlad Chirita Alexandru TheEpicWipedCrea Data 26 noiembrie 2022 12:22:28
Problema Perle Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in  ("perle.in");
ofstream out("perle.out");

#define maxN 10000

int n,pos;
int v[maxN+1];

bool A();
bool B();
bool C();

bool A(){
    if(pos>n){
        return 0;
    }
    pos++;
    return 1;
}

bool B(){
    if(pos>n){
        return 0;
    }
    if(v[pos]==3){
        pos++;
        return 0;
    }
    if(v[pos]==2){
        pos++;
        B();
    }
    if(v[pos]==1 && v[pos+2]==3){
        pos++;
        if(A()==0){
            return 0;
        }
        pos++;
    }
    return A() && C();
}

bool C(){
    if(pos>n){
        return 0;
    }
    if(v[pos]==2){
        pos++;
        return 1;
    }
    if(v[pos]==1){
        if(v[pos+1]==2){
            pos+=2;
            return A();
        }
        return 0;
    }
    pos++;
    return B() && C();
}

int main(){
    int q;
    in>>q;
    for(int z=1;z<=q;z++){
        in>>n;
        for(int i=1;i<=n;i++){
            in>>v[i];
        }
        pos=1;
        if(A() && pos>n){
            out<<1<<'\n';
        }
        else if(B() && pos>n){
            out<<1<<'\n';
        }
        else if(C() && pos>n){
            out<<1<<'\n';
        }
        else{
            out<<0<<'\n';
        }
    }
}