#include <stdio.h>
#define MAX 6
#define UP 1
#define DOWN 2
#define RIGHT 3
#define LEFT 4
int main()
{
int Test[MAX][MAX];
int i,j,Count=0;
int Direction=UP;
int Row=0,Col=0,ReverseRow=MAX-1,ReverseCol=MAX-1;
for(i=0; i<MAX; i++)
{
for(j=0; j<MAX; j++)
{
Test[i][j]=0;
}
}
while(Count<MAX*MAX+1)
{
switch(Direction)
{
case UP:
{
i=Row++;
for(j=0; j<MAX-1; j++)
{
if(Test[i][j]==0)
Test[i][j]=Count++;
}
Direction=RIGHT;
break;
}
case RIGHT:
{
j=ReverseCol--;
for(i=0; i<MAX; i++)
{
if(Test[i][j]==0)
Test[i][j]=Count++;
}
Direction=DOWN;
break;
}
case DOWN:
{
i=ReverseRow--;
for(j=MAX-1-1; j>0; j--)
{
if(Test[i][j]==0)
Test[i][j]=Count++;
}
Direction=LEFT;
break;
}
case LEFT:
{
j=Col++;
for(i=MAX-1; i>0; i--)
{
if(Test[i][j]==0)
Test[i][j]=Count++;
}
Direction=UP;
break;
}
default:
{
break;
}
}
}
for(i=0; i<MAX; i++)
{
for(j=0; j<MAX; j++)
{
printf("%d\t",Test[i][j]);
}
printf("\n");
}
}