#include<iostream.h>
class NumPuzzle
{
public:
int x,i,j,m,count;
int puz[25][25];
void numpuzz();
void putData();
NumPuzzle();
};
NumPuzzle::NumPuzzle()
{
for(i=0;i<25;i++)
for(j=0;j<25;j++)
puz[i][j]=0;
cout<<"Enter the Odd number between 1 to 27:";
a:
cin>>x;
if(x%2==0)
{
cout<<endl<<"Enter the Odd number:";
goto a;
}
}
void NumPuzzle::putData()
{
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
cout<<puz[i][j];
cout<<" "<<" ";
}
cout<<endl<<endl;
}
}
void NumPuzzle::numpuzz()
{
m=x/2;
count=1;
while(puz[x-1][x/2]==0)
{
if(puz[0][x/2]==0)
{
puz[0][x/2]=count;
count++;
j=0;
}
else if(j==0&&m<x)
{
m++;
j=x-1;
if(m==x)
{
m=x-1;
j=1;
}
puz[j][m]=count;
count++;
if((m==(x-1))&&(j==x/2-1)&&puz[x-1][x/2-1]!=0)
{
m=m+1;
j=x-1;
puz[j][m]=count;
count++;
}
}
else if(m==x-1&&j>0)
{
m=0;
j=j-1;
puz[j][m]=count;
count++;
}
else if(j<=x-1&&m>=0&&j!=0)
{
if(puz[j-1][m+1]==0&&m!=x)
{
m=m+1;
j=j-1;
puz[j][m]=count;
count++;
}
else if(puz[j-1][m+1]!=0||m==x)
{
j=j+1;
puz[j][m]=count;
count++;
}
}
}
}
void main()
{
char ch='y';
do
{
NumPuzzle venki;
venki.numpuzz();
venki.putData();
cout<<endl<<"If you want ot continue press 'y':"<<endl;
cin>>ch;
}while(ch=='y');
cout<<endl<<" thank you ";
}