Pagini recente » Cod sursa (job #118760) | Cod sursa (job #1237008)
#include <fstream>
using namespace std;
ifstream fin ("perle.in");
ofstream fout ("perle.out");
int N, L, step, ok, V[10010];
bool Recursiv_C();
bool Recursiv_B()
{
// B -> 2B | 1A3AC
if (step > L) return 0;
if (V[step] == 2)
{
step += 1;
return Recursiv_B();
}
if (V[step] == 1 && V[step+2] == 3)
{
step += 4;
return Recursiv_C();
}
return 0;
}
bool Recursiv_C()
{
// C -> 2 | 3BC | 12A
if (step > L) return 0;
if (V[step] == 2) return 1;
if (V[step] == 3)
{
step += 1;
if (Recursiv_B()) return Recursiv_C();
}
if (V[step] == 1 && V[step+1] == 2)
{
step += 3;
return 1;
}
return 0;
}
int main()
{
fin >> N;
for (int i=1; i<=N; i++)
{
fin >> L;
for (int j=1; j<=L; j++) fin >> V[j];
step = 1;
if (step == L) fout << "1\n";
else if (V[step] == 2 || (V[step] == 1 && V[step+2] == 3)) fout << Recursiv_B() << '\n';
else fout << Recursiv_C() << '\n';
}
fout.close();
return 0;
}