Cod sursa(job #2590471)

Utilizator EckchartZgarcea Robert-Andrei Eckchart Data 28 martie 2020 00:14:23
Problema Perle Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.78 kb
#include <fstream>
#include <ios>
#include <cstring>
#include <string>
using US = unsigned short;
US N, len_arr;
US arr[10000];
US pos;
bool ok = true;


void C();


void B()
{
    if (pos >= len_arr || arr[pos] == 3)
    {
        ok = false;
        return;
    }
    
    if (arr[pos] == 1 && arr[pos + 2] == 3)
    {
        pos += 4;
        C();
    }
    else if (arr[pos] == 2)
    {
        ++pos;
        B();
    }
    else
    {
        ok = false;
    }
}


void C()
{
    if (pos >= len_arr || arr[pos] == 2)
    {
        return;
    }
    if (arr[pos] == 1)
    {
        if (arr[pos + 1] == 2)
        {
            pos += 2;
            return;
        }
        else
        {
            ok = false;
            return;
        }
        
    }

    ++pos;
    B();
    ++pos;
    C();
}


int main()
{
    std::ios_base::sync_with_stdio(false);
    std::ifstream fisierIN("perle.in");
    std::ofstream fisierOUT("perle.out");
    fisierIN.tie(NULL);
    fisierOUT.tie(NULL);

    fisierIN >> N;
    for (US i = 0; i < N; ++i)
    {
        fisierIN >> len_arr;
        for (US j = 0; j < len_arr; ++j)
        {
            fisierIN >> arr[j];
        }

        if (len_arr == 1 || (arr[1] == 2 && len_arr == 3))
        {
            fisierOUT << "1\n";
            continue;
        }

        if (arr[0] == 1 && arr[2] == 3)
        {
            pos += 4;
            C();
        }
        else if (arr[0] == 2)
        {
            ++pos;
            B();
        }
        else
        {
            ++pos;
            B();
            ++pos;
            C();
        }
        
        fisierOUT << (pos == len_arr - 1 && ok) << "\n";
        ok = true;
        pos = 0;
    }
}