int main(void) {
int n,i,r0,c0,sp;
scanf("%d",&n);
int a[n][2];
for(i=0;i<n;i++)
{
scanf("%d %d",&a[i][0],&a[i][1]);
}
int ans[50][2]=
{
1,1,
2,2,
1,3,
2,4,
3,5,
4,6,
5,7,
6,8,
7,7,
8,6,
7,5,
6,4,
5,3,
4,2,
3,1,
2,2,
3,3,
2,4,
1,5,
2,6,
3,7,
4,8,
5,7,
6,6,
7,5,
8,4,
7,3,
6,2,
5,1,
4,2,
3,3,
4,4,
3,5,
2,6,
1,7,
2,8,
3,7,
4,6,
5,5,
6,4,
7,3,
8,2,
7,1,
6,2,
5,3,
4,4,
5,5,
6,6,
7,7,
8,8
};
for(i=0;i<n;i++)
{
r0=a[i][0];
c0=a[i][1];
for(sp=0;sp<50;sp++)
{
if((r0==ans[sp][0])&&(c0==ans[sp][1]))
break;
}
printf("49\n");
for(i=sp+1;i<50;i++)
printf("%d %d\n",ans[i][0],ans[i][1]);
for(i=0;i<sp;i++)
printf("%d %d\n",ans[i][0],ans[i][1]);
}
return 0;
}
link to question - ADASHOP2 Problem - CodeChef
link to my submission - CodeChef: Practical coding for everyone
It gives a partially correct answer.
Note that each cell may be visited multiple times and it is not necessary to return to the starting point.