Cod sursa(job #1466741)

Utilizator andrei.arnautuAndi Arnautu andrei.arnautu Data 30 iulie 2015 01:40:29
Problema Perle Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
/**
  *  Worg
  */
#include <cstdio>
#include <queue>

#define DIM 10010

using namespace std;
FILE *fin=freopen("perle.in","r",stdin);
FILE *fout=freopen("perle.out","w",stdout);

int n;
int V[DIM];
queue <int> Q;

void read_data() {

    scanf("%d ", &n);
    for(int i = 1; i <= n; ++i)
        scanf("%d ", &V[i]);
}

bool check() {

    for(int pos = 1; pos <= n; ++pos) {

        if( Q.empty() )
            return 0;

        if( Q.front() <= 3 && V[pos] != Q.front() )
            return 0;

        if( Q.front() == 5 ) {

            if( V[pos] == 3 )
                return 0;
            if( V[pos] == 2 ) {
                Q.push(5);
            }
            else {
                Q.push(4); Q.push(3); Q.push(4); Q.push(6);
            }
        }

        if( Q.front() == 6 ) {

            if( V[pos] == 1 ) {
                Q.push(2); Q.push(4);
            }

            if( V[pos] == 3 ) {
                Q.push(5); Q.push(6);
            }
        }

        Q.pop();
        if(pos + Q.size() > n)
            return 0;
    }

    return 1;
}

void clean() {

    while( !Q.empty() )
        Q.pop();
}

int main() {

    int t;
    bool ans;
    for(scanf("%d", &t); t; --t) {

        read_data(); ans = 0;
        Q.push(4); ans = max(ans, check()); clean();
        Q.push(5); ans = max(ans, check()); clean();
        Q.push(6); ans = max(ans, check()); clean();
        printf("%d\n", ans);
    }
    return 0;
}