Cod sursa(job #2116996)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 28 ianuarie 2018 13:21:18
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>

using namespace std;

ifstream fin ("perle.in"); ofstream fout ("perle.out");

const int nmax = 1e4;

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

void C ();

void A () {
    if (pos != 0)
        ++ pos;
}

void B () {
    if (pos == 0)
        return ;

    if (v[ pos ] == 2) {
        ++ pos;
        B ();
    } else if (v[ pos ] == 1) {
        ++ pos;
        A ();

        if (v[ pos ] != 3) {
            pos = 0;
            return ;
        }

        ++ pos;
        A ();
        C ();
    } else {
        pos = 0;
    }
}

void C () {
    if (pos == 0)
        return ;

    if (v[ pos ] == 2) {
        ++ pos;
    } else if (v[ pos ] == 1) {
        ++ pos;
        if (v[ pos ] != 2) {
            pos = 0;
            return ;
        }

        ++ pos;
        A ();
    } else {
        ++ pos;
        B ();
        C ();
    }
}

void solve () {
    fin >> n;

    for (int i = 1; i <= n; ++ i)
        fin >> v[ i ];

    bool ok = 0;
    pos = 1; A ();
    if (pos == n + 1)
        ok = 1;

    pos = 1; B ();
    if (pos == n + 1)
        ok = 1;

    pos = 1; C ();
    if (pos == n + 1)
        ok = 1;

    fout << ok << "\n";
}

int main () {
    int t;
    fin >> t;

    while (t --) {
        solve ();
    }

    fin.close();
    fout.close();
    return 0;
}