Pagini recente » Cod sursa (job #207749) | Cod sursa (job #1713659) | Cod sursa (job #2066158) | Cod sursa (job #1930292) | Cod sursa (job #1796351)
#include <stdio.h>
#define MAX_LENGHT 10010
FILE* fin;
FILE* fout;
int temp[MAX_LENGHT] = { 0 };
int nre[MAX_LENGHT] = { 0 };
int sol[MAX_LENGHT] = { 0 };
int n;
int crtLgh;
bool poss;
void LoadFiles()
{
fin = fopen("perle.in", "r");
fout = fopen("perle.out", "w");
}
void Init()
{
LoadFiles();
}
void ReadNre()
{
for(int i=0;i<crtLgh;i++)
{
fscanf(fin, "%d", &nre[i]);
}
}
void Complete(int ix, int type)
{
int ramIndex;
int bIndex = ix+1;
int tIndex = 0;
while(sol[bIndex] != 0)
{
temp[tIndex++] = sol[bIndex++];
}
switch(type)
{
case 0:
sol[ix] = 1;
ramIndex = ix+1;
break;
case 1:
sol[ix] = 2;
ramIndex = ix+1;
break;
case 2:
sol[ix] = 3;
ramIndex = ix +1;
break;
case 3:
sol[ix] = 2;
sol[ix+1] = 'B';
ramIndex = ix + 2;
break;
case 4:
sol[ix] = 1;
sol[ix+1] = 'A';
sol[ix+2] = 3;
sol[ix+3] = 'A';
sol[ix+4] = 'C';
ramIndex = ix + 5;
break;
case 5:
sol[ix] =2;
ramIndex = ix + 1;
break;
case 6:
sol[ix] = 3;
sol[ix+1] = 'B';
sol[ix+2] = 'C';
ramIndex = ix + 3;
break;
case 7:
sol[ix] = 1;
sol[ix+1] = 2;
sol[ix+2] = 'A';
ramIndex = ix + 3;
break;
}
if(tIndex != 0)
{
for(int i=0;i<tIndex;i++)
{
sol[ramIndex++] = temp[i];
}
}
}
void ClearFrom(int index)
{
for(int i=index;i<=crtLgh;i++)
{
sol[i] = 0;
}
}
void Possible(int index)
{
if(!poss)
{
if(index == 0)
{
for(int i=0;i<8;i++)
{
Complete(index, i);
if(sol[index] == nre[index])
Possible(index+1);
}
}
else
{
if(index == crtLgh && sol[index] == 0)
{
poss = true;
}
else
{
if(index < crtLgh)
{
if(sol[index] == nre[index])
{
Possible(index+1);
}
else
{
if(sol[index] == 'A')
{
Complete(index, nre[index]-1);
Possible(index);
}
else
{
if(sol[index] == 'B')
{
Complete(index, 3);
Possible(index);
Complete(index, 4);
Possible(index);
}
else
{
if(sol[index] == 'C')
{
Complete(index, 5);
Possible(index);
Complete(index, 6);
Possible(index);
Complete(index, 7);
Possible(index);
}
else
{
ClearFrom(index);
}
}
}
}
}
}
}
}
}
void Write(bool val)
{
fprintf(fout, "%d\n",val);
}
void Read()
{
fscanf(fin, "%d", &n);
for(int i=0;i<n;i++)
{
fscanf(fin, "%d", &crtLgh);
ReadNre();
poss = false;
Possible(0);
Write(poss);
ClearFrom(0);
}
}
void CloseFiles()
{
fclose(fin);
fclose(fout);
}
void Terminate()
{
CloseFiles();
}
int main(void)
{
Init();
Read();
Terminate();
return 0;
}