Pagini recente » Cod sursa (job #1236891) | Cod sursa (job #239449) | Cod sursa (job #625144) | Cod sursa (job #2341947) | Cod sursa (job #189650)
Cod sursa(job #189650)
#include <iostream>
#include <fstream>
using namespace std;
int L;
int V[10001];
int pos;
bool tryB();
bool tryC();
bool tryA()
{
if (pos >= L)
return false;
++pos;
return true;
}
bool tryB()
{
if (pos >= L)
return false;
if (V[pos] == 2) {
++pos;
return tryB();
}
if (V[pos] == 1) {
++pos;
if (!tryA())
return false;
if (V[pos++] != 3)
return false;
if (!tryA())
return false;
return tryC();
}
return false;
}
bool tryC()
{
if (pos >= L)
return false;
if (V[pos] == 2) {
++pos;
return true;
}
if (V[pos] == 3) {
++pos;
if (!tryB())
return false;
return tryC();
}
if (V[pos] == 1) {
++pos;
if (V[pos++] != 2)
return false;
return tryA();
}
return false;
}
bool canMake()
{
if (L == 1)
return true;
if (L == 2)
return false;
pos = 0;
if (tryB() && (pos == L))
return true;
pos = 0;
return (tryC() && (pos == L));
}
int main(int argc, char *argv[])
{
FILE *fi = fopen("perle.in", "r");
int N;
fscanf(fi, "%d", &N);
ofstream fout("perle.out");
while (N--) {
fscanf(fi, "%d", &L);
for (int i(0); i < L; ++i)
fscanf(fi, "%d", V+i);
fout << canMake() << endl;
}
fout.close();
fclose(fi);
return 0;
}