#include<stdio.h>
int l;
int check(int m[][l],int a,int r,int c,int e)
{
int t,i,j;
for(i=0;i<c;i++)
{
if(m[a][i]==e)
return 1;
}
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
if(a!=j)
{
if(m[a][i]==m[j][i]&&m[a][i]!=-1)
{
m[a][i]=-1;
return check(m,j,r,c,e);
}
}
}
}
return 0;
}
int main()
{
int t;
int i,j;
scanf("%d",&t);
for(t;t>0;t--)
{
l=0;
int n,e,a,b,x,y;
scanf("%d%d%d%d",&n,&e,&a,&b);
int m[e][n];
l=n;
for(i=0;i<e;i++)
{
for(j=0;j<n;j++)
{
m[i][j]=-1;
}
}
for(i=0;i<e;i++)
{
scanf("%d%d",&x,&y);
for(j=0;j<((n-y)/x)+1;j++)
{
m[i][y+(j*x)]=y+(j*x);
}
}
int v,g=0;
for(i=0;i<e;i++)
{
for(j=0;j<n;j++)
{
if(m[i][j]==a)
{
v=check(m,i,e,n,b);
g++;
break;
}
}
}
if(v==1)
{
printf("It is possible to move the furniture.\n");
}
else if(v==0||g==0)
printf("The furniture cannot be moved.\n");
}
return 0;
}
“I’m getting the correct answer for value of n<=50000, when i give more values i’m getting runtime error”
“i have written a code for “http://www.spoj.pl/problems/SCRAPER/”..
when i run it in ideone, i got the answers as “runtime error” for more ‘n’ values
You are trying to allocate more memory than it’s available on the stack which causes stack overflow. Usually it is much better to allocate huge arrays like that dynamically by using
malloc,callocorrealloc.Check this question: C: Array initialization segfaults depending on size and call to printf()