Cod sursa(job #531468)
#include <fstream>
using namespace std;
const char InFile[]="perle.in";
const char OutFile[]="perle.out";
const int MaxL=10111;
ifstream fin(InFile);
ofstream fout(OutFile);
int N,L,v[MaxL];
int C(int pos);
int B(int pos);
int C(int pos)
{
if(pos>L)
{
return 0;
}
if(v[pos]==2)
{
if(pos+2<=L)
{
return pos+2;
}
return 0;
}
if(v[pos]==3)
{
if(pos+2>L)
{
return 0;
}
pos=B(pos+1);
if(pos>0 && pos<=L)
{
return C(pos);
}
}
if(pos+3<=L)
{
return pos+3;
}
return 0;
}
int B(int pos)
{
if(pos>L)
{
return 0;
}
if(v[pos]==2)
{
return B(pos+1);
}
else if(v[pos]==1 && v[pos+2]==3 && pos+2<=L)
{
return C(pos+4);
}
return 0;
}
int main()
{
fin>>N;
for(register int i=0;i<N;++i)
{
fin>>L;
for(register int j=1;j<=L;++j)
{
fin>>v[j];
}
if(B(1) || C(1) || L==1)
{
fout<<"1";
}
else
{
fout<<"0";
}
fout<<"\n";
}
fin.close();
fout.close();
return 0;
}