Cod sursa(job #881047)

Utilizator otto1Palaga Vicentiu-Octavian otto1 Data 17 februarie 2013 17:39:43
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

int n, t, v[20000];

int B(int x);
int C(int x);

int B(int x)
{
    if(x > n)
        return 0;
    if(v[x] == 2)
        return B(x+1);
    if(v[x]==1 && v[x+2]==3)
        return C(x+4);
    return 0;
}

int C(int x)
{
    if(x > n)
        return 0;
    if(v[x] == 2)
        return x;
    if(v[x]==1 && v[x+1]==2)
        return x+2;
    if(v[x] == 3)
    {
        int aux = B(x+1);
        if(aux)
            return C(aux+1);
    }
    return 0;
}

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

    //Read
    fin>>t;

    //Compute
    for(int i=1; i<=t; i++)
    {
        fin>>n;
        for(int j=1; j<=n; j++)
            fin>>v[j];
        if(n==1 || n==B(1) || n==C(1))
            fout<<"1"<<'\n';
        else
            fout<<"0"<<'\n';
    }
}