why Runtime Error(SIGSEGV) ???

solution of snake snaky

#include<stdio.h>
int move(int map[1001][1001],int l,char ch,int sx,int sy,int cord[][2],int &status,int &stat)
{
int temp=0;
int &c=stat;
map[cord[c][0]][cord[c][1]]=0;
if(c==0)
temp=l-1;
else
temp=c-1;
if(ch=='L')
{
cord[c][0]=cord[temp][0]-1;
cord[c][1]=cord[temp][1];
}
if(ch=='R')
{
cord[c][0]=cord[temp][0]+1;
cord[c][1]=cord[temp][1];
}
if(ch=='D')
{
cord[c][0]=cord[temp][0];
cord[c][1]=cord[temp][1]-1;
}
if(ch=='U')
{
cord[c][0]=cord[temp][0];
cord[c][1]=cord[temp][1]+1;
}
if(cord[c][0]<1 || cord[c][0]>sx || cord[c][1]<1 || cord[c][1]>sy)
{
status=1;return 0;
}
else if(map[cord[c][0]][cord[c][1]]==-1)
{status=0;return 0;
}
else
map[cord[c][0]][cord[c][1]]=-1;
c++;
return 1;
}
int main()
{
int t;
scanf("%d",&t);
while(t)
{
int sx,sy,tx,ty,l,cord[1000000][2]={0,0},map[1001][1001]={0},status=0,count=0,stat=0;
char pos[1000000]={'\0'},ch;
status=0;count=0;stat=0;
scanf("%d%d%d%d%d",&sx,&sy,&tx,&ty,&l);
scanf("%s",pos);
map[tx][ty]=-1;
cord[0][0]=tx;
cord[0][1]=ty;
for(int i=0;pos[i]!='\0';i++)
{
if(pos[i]=='L')
{
   map[tx-1][ty]=-1;tx--;
   cord[i+1][0]=tx;
   cord[i+1][1]=ty;
}
if(pos[i]=='R')
{
map[tx+1][ty]=-1;tx++;
cord[i+1][0]=tx;
cord[i+1][1]=ty;
}
if(pos[i]=='D')
{
map[tx][ty-1]=-1;ty--;
cord[i+1][0]=tx;
cord[i+1][1]=ty;
}
if(pos[i]=='U')
{
 map[tx][ty+1]=-1;ty++;
 cord[i+1][0]=tx;
 cord[i+1][1]=ty;
}
ch=pos[i];
}
count=0;
stat=0;
while(move(map,l,ch,sx,sy,cord,status,stat))
{
    count++;
	stat=stat%l;
}
if(status==1)
printf("WALL %d\n",count);
if(status==0)
printf("BODY %d\n",count);
t--;
}
}

Runtime error is not because you have taken 3 arrays of size 106 but because you have declared them inside main()…There is separate memory area for global variables…and arrays this big should be declared globally…

2 Likes

is it because i have taken 3 arrays of 10^6 size???

you can see the answer to similar type of problem here

but this doesnt make any difference