Cod sursa(job #2099397)

Utilizator robx12lnLinca Robert robx12ln Data 4 ianuarie 2018 13:03:36
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include<fstream>
using namespace std;
ifstream fin("perle.in");
ofstream fout("perle.out");
int M, N, v[10005], pos;

int B();
int C();

int B(){
    if( v[pos] == 2 && pos + 1 <= N ){
        pos++;
        return B();
    }
    if( v[pos] == 1 && v[pos + 2] == 3 && pos + 4 <= N ){
        pos += 4;
        return C();
    }
    return 0;
}

int C(){
    if( v[pos] == 2 && pos <= N ){
        pos++;
        return 1;
    }
    if( v[pos] == 1 && v[pos + 1] == 2 && pos + 2 <= N ){
        pos += 3;
        return 1;
    }
    if( v[pos] == 3 && pos + 2 <= N ){
        pos++;
        if( B() == 1 )
            return C();
    }else
        return 0;
}

int main(){
    fin >> M;
    for( int i = 1; i <= M; i++ ){
        fin >> N;
        for( int j = 1; j <= N; j++ )
            fin >> v[j];
        if( N == 1 ){
            fout << "1\n";
            continue;
        }
        pos = 1;
        int ok = B();
        if( pos == N + 1 )
            fout << "1\n";
        else{
            pos = 1;
            ok = C();
            if( pos == N + 1 ){
                fout << "1\n";
            }else{
                fout << "0\n";
            }
        }
    }
    return 0;
}