Cod sursa(job #2298434)

Utilizator Mirela_MagdalenaCatrina Mirela Mirela_Magdalena Data 8 decembrie 2018 10:24:13
Problema Perle Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.14 kb
#include <cstdio>
#include <cstring>
using namespace std;

struct prl{
    char a[20][20];
    int nr=0;
};
prl b[4];

int n, li;
int fin[10001];
int parc[10001];


void init()
{
    b[1].nr = 3;
    strcpy(b[1].a[1], "1");
    strcpy(b[1].a[2], "2");
    strcpy(b[1].a[3], "3");
    b[2].nr = 2;
    strcpy(b[2].a[1], "1A3AC");
    strcpy(b[2].a[2], "2B");
    b[3].nr = 3;
    strcpy(b[3].a[1], "12A");
    strcpy(b[3].a[2], "2");
    strcpy(b[3].a[3], "3BC");
}

int cursor = 0;
int verif(char ch, int k)
{
    if(cursor == li)
        return 1;
    if(cursor > li)
        return 0;
    if(k == 0)
    {
        if(b[2].nr >= fin[cursor])
        {
            int aux = cursor;
            int poz = fin[cursor];
            int j=0;
            while(b[2].a[poz][j] != '\0')
            {
                if(b[2].a[poz][j]-'0' == fin[cursor] || b[2].a[poz][j] == 'A')
                    cursor++;
                else{
                    if(verif(b[2].a[poz][j], k+1) == 1)
                        return 1;
                }
                j++;
            }
            if(cursor == li)
                return 1;
        }
        else if(b[3].nr >= fin[cursor])
        {
            int aux = cursor;
            int poz = fin[cursor];
            int j=0;
            while(b[3].a[poz][j] != '\0')
            {
                if(b[3].a[poz][j]-'0' == fin[cursor] || b[3].a[poz][j] == 'A')
                    cursor++;
                else{
                    if(verif(b[2].a[poz][j], k+1) == 1)
                        return 1;
                }
            }
            if(cursor == li)
                return 1;
        }
    }
    else{
        if(ch == 'B')
        {
            int aux = cursor;
            int poz = fin[cursor];
            int j=0;
            while(b[2].a[poz][j] != '\0')
            {
                if(b[2].a[poz][j]-'0' == fin[cursor] || b[2].a[poz][j] == 'A')
                    cursor++;
                else{
                    if(verif(b[2].a[poz][j], k+1) == 1)
                        return 1;
                }
                j++;
            }
            if(cursor == li)
                return 1;
        }
        else{
            int aux = cursor;
            int poz = fin[cursor];
            int j=0;
            while(b[3].a[poz][j] != '\0')
            {
                if(b[3].a[poz][j]-'0' == fin[cursor] || b[3].a[poz][j] == 'A')
                    cursor++;
                else{
                    if(verif(b[2].a[poz][j], k+1)==1)
                        return 1;
                }
                j++;
            }
            if(cursor == li)
                return 1;

        }

    }
}




void cit()
{
    scanf("%d", &n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d", &li);
        for(int i=0; i<li; i++)
            scanf("%d", &fin[i]);
        if(li == 1)
            printf("1\n");
        else printf("%d\n", verif('0', 0));
    }
}




int main()
{
    freopen("perle.in", "r", stdin);
    freopen("perle.out", "w", stdout);
    init();
    cit();
    return 0;
}