<PRE>
也谈螺旋矩阵的生成
===================================================================
问题描述:给出n,要求打印顺时针或者逆时针螺旋矩阵,例如:
n = 3:
1 8 7
2 9 6
3 4 5
n = 4:
1 12 11 10
2 13 16 9
3 14 15 8
4 5 6 7
现在实现逆时针的螺旋矩阵。首先我们考虑填充的方向,开始从上到下,每次到达边界以后改变
方向(左转弯),假设Dir表示前进的方向,则有:
//Dir:
// 0: down; 1: right; 2: up; 3:left
void ChangeDirection( int& Dir)
{
Dir ++;
if(Dir>3)
Dir = 0;
}
其次,边界是什么概念?当然首先是到达矩阵的边缘,或者是遇到矩阵里面已经有数据了,就必须
改变方向了(无需证明)。因此有下面的位置判别函数:
typedef struct tagPos
{
int x;
int y;
}POS;
//return 0 means Position not valid
//Data: the metrics data
//n : the metrics dimension
int PosIsValid(int *Data, POS &p, int n)
{
if( p.x<0 || p.x>=n )return 0;
if( p.y<0 || p.y>=n )return 0;
if( Data[ p.y*n + p.x ] != 0 )return 0;
return 1;
}
有了以上两点,获取下一个填充位置就很方便了:
//Data : the Metrics
//p : Current position
//Dir : Filling direction
//n : Dimension of the metircs
void GetNextPos(int *Data, POS &p , int& Dir, int n)
{
POS next;
for(;;)
{
//calculate the next position from p
next.x = p.x; next.y = p.y;
switch(Dir)
{
case 0: //down
next.y++; break;
case 1: //right
next.x++; break;
case 2: //up
next.y--; break;
case 3: //left
next.x--; break;
}
//determine whether the next position is valid
if(!PosIsValid(Data, next, n))
{
//if the new position is not valid, that means
//we must change the direction,and continue get
//the new position
ChangeDirection(Dir);
}
else break;
}
//change current position to new position
p.x = next.x;
p.y = next.y;
}
一切准备就绪,我们现在可以给出矩阵的填充函数了:
//Data : the Metrics
//n : Metrics Dimension
//Dir : Filling Operation Direction
void FillMatrics(int *Data, int n, int Dir)
{
POS p;
p.x = p.y = 0; //first position at point (0,0)
for(int i=1; i<=n*n; i++)
{
int index = p.y * n + p.x; //calculate position
Data[index] = i;
if(i<n*n)
GetNextPos(Data, p, Dir, n);
}
}
写个主程序测试一下:
#include <stdio.h>
void main()
{
int n;
printf("\nPlease input the metrics dimension:");
scanf("%d",&n);
int *data = new int[n*n];
//we must initialize the metrics with zero.
for(int i=0;i<n*n; i++)data[i]=0;
FillMatrics(data,n,0);
//print the result!
printf("\nThe Result :\n");
for( i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
printf("%d\t ", data[i*n+j]);
}
printf("\n");
}
delete[] data;
}
//================================End=======================
//iwaswzq 2004/12/14
</PRE>