#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int a[n][n];
for (int i = 0; i < n*n; i++)
{
a[i/n][i%n] = 0; //initialize matrix a.
}
int b;
b = (n-1)/2;
a[0][b] = 1; //initialize the first number.
for (int k = 2; k <= n*n; k++)
{
for (int i = 0; i < n; i++)
{
if (a[0][i] == k-1) //case if (k-1) in first row
{
if (i != n-1) //not in last column (1)
{
a[n-1][i+1] = k;
}
else //in last column (3)
{
a[1][n-1] = k;
}
}
else if (a[i][n-1] == k-1 && i != 0) //case if k-1 is in the last column but not first row (2)
{
a[i-1][0] = k;
}
else //case if k-1 is not in row 1 nor in column n (4)
{
for (int j = 0; j < n-1; j++)
{
if (a[i][j] == k-1)
{
if (a[i-1][j+1] == 0)
{
a[i-1][j+1] = k;
}
else
{
a[i+1][j] = k;
}
}
}
}
}
}
for (int i = 0; i < n*n; i++)
{
printf("%d ", a[i/n][i%n]);
if (i%n == n-1)
{
printf("\n");
}
}
return 0;
}
// The output still contains 0 and doesn't contain the last number. How can I fix it?