Cod sursa(job #1940462)

Utilizator ApostolIlieDanielApostol Daniel ApostolIlieDaniel Data 26 martie 2017 17:01:14
Problema Perle Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.15 kb
#include <stdio.h>

using namespace std;
char v[10001];
char st[10001];
int main()
{
    FILE *fin, *fout;
    int n, i, j, e, ok, l;
    fin = fopen ("perle.in", "r");
    fout = fopen ("perle.out", "w");
    fscanf (fin, "%d", &n);
    for (i = 1; i <= n ;i++) {
        fscanf (fin, "%d", &l);
        for (j = 1; j <=  l;j++)
            fscanf (fin, "%d", &v[j]);
        st[1] = 'B';
        j = 1;
        e = 1;
        ok = 0;
        while (j > 0 && e <= l && ok == 0) {
            if (v[e] == 1) {
                if (st[j] == 'A') {
                    e++;
                    j--;
                }
                if (st[j] == 'B'){
                    e++;
                    st[j] = 'C';
                    j++;
                    st[j] = 'A';
                    j++;
                    st[j] = 3;
                    j++;
                    st[j] = 'A';
                }
                if (st[j] == 'C') {
                    e++;
                    st[j] = 'A';
                    j++;
                    st[j] = 2;
                }
            }
            if (v[e] == 2) {
                if (st[j] == 'A') {
                    e++;
                    j--;
                }
                if (st[j] == 'B') {
                    e++;
                }
                if (st[j] == 'C') {
                    j--;
                    e++;
                }
            }
            if (v[e] == 3) {
                if (st[j] == 'A') {
                    e++;
                    j--;
                }
                if (st[j] == 'B') {
                    j = 0;
                    ok = 1;
                }
                if (st[j] == 'C') {
                    e++;
                    st[j] = 'C';
                    j++;
                    st[j] = 'B';
                }
            }
            while (v[e] == st[j] && j > 0 && e <= l) {
                e++;
                j--;
            }
        }
        if (e > l && ok == 0){
            fprintf (fout, "1\n");
        }
        else {
            st[1] = 'C';
            j = 1;
            e = 1;
            ok = 0;
            while (j > 0 && e <= l && ok == 0) {
                if (v[e] == 1) {
                    if (st[j] == 'A') {
                        e++;
                        j--;
                    }
                    if (st[j] == 'B'){
                        e++;
                        st[j] = 'C';
                        j++;
                        st[j] = 'A';
                        j++;
                        st[j] = 3;
                        j++;
                        st[j] = 'A';
                    }
                    if (st[j] == 'C') {
                        e++;
                        st[j] = 'A';
                        j++;
                        st[j] = 2;
                    }
                }
                if (v[e] == 2) {
                    if (st[j] == 'A') {
                        e++;
                        j--;
                    }
                    if (st[j] == 'B') {
                        e++;
                    }
                    if (st[j] == 'C') {
                        j--;
                        e++;
                    }
                }
                if (v[e] == 3) {
                    if (st[j] == 'A') {
                        e++;
                        j--;
                    }
                    if (st[j] == 'B') {
                        j = 0;
                        ok = 1;
                    }
                    if (st[j] == 'C') {
                        e++;
                        st[j] = 'C';
                        j++;
                        st[j] = 'B';
                    }
                }
                while (v[e] == st[j] && j > 0 && e <= l) {
                    e++;
                    j--;
                }
            }
            if (e > l && ok == 0) {
                fprintf (fout, "1\n");
            }
            else
                fprintf (fout, "0\n");
        }
    }
    fclose (fin);
    fclose (fout);
    return 0;
}