Cod sursa(job #1640677)

Utilizator danutbodbodnariuc danut danutbod Data 8 martie 2016 18:51:13
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>
using namespace std;
ifstream fin("perle.in");
ofstream fout("perle.out");
int n,k,a[10009];
int B(int x);
int C(int x);
//A -> 1 | 2 | 3

//B -> 2B | 1A3AC
int B(int x){
    if(a[x]==2) return B(x+1);  //2B
    if(a[x]==1 && a[x+2]==3) return C(x+4); //1A3AC
    return 0;
}

//  C -> 2 | 3BC | 12A
int C(int x){
    if(a[x]==2) return x+1;   //2
    if(a[x]==3) return C(B(x+1)); //3BC
    if(a[x]==1 && a[x+1]==2) return x+3; //12A

}
int main()
{   fin>>n;
    for(int i=1;i<=n;i++){
        fin>>k;
        for(int j=1;j<=k;j++)fin>>a[j];
        if(k==1 || B(1)==k+1 || C(1)==k+1) fout<<1<<'\n';
         else fout<<0<<'\n';
    }
    return 0;
}