Spiral matrix (queue)


There is the following matrix. Let the coordinates of 1 point be (0, 0), the coordinates of 7 points are (-1, -1), and the coordinates of 2 points are (0, 1). The coordinate values ​​are input by programming, such as (a, b) ), output its corresponding value

Observing the matrix, knowing that this matrix is ​​spirally expanding outwards in a clockwise direction, we can divide it into a circle, as follows:


Now let's see which lap (a, b) will be in? Point 2 (0, 1) is in the second circle, and point 18 (-1, -2) is in the second circle, so the number of circles is equal to max (abs(a), abs(b)), that is, take a, b The largest one in absolute value. And the largest number in each lap is 1, 9, 25. What is the law between them?

1=1^2; 9= 3^2; 25 = 5^2; and 1, 3, 5 are related to the number of turns, set the number of turns=c, then they are equal to 2c+1, so the maximum value MaxValue=(2c +1)^2;

The last step is to observe the values ​​of the top, bottom, left, and right sides in a circle, how do they calculate


Let’s take the third circle as an example to find the base value of each side of the third circle, because one of their coordinates (a, b) must be 0, which is convenient for calculation, so we regard them as the base value

For the above, y is unchanged and x is changing, that is, when y=-2(y=-c), 23=25+(-2), so the upper base value topBase=MaxValue+y, because the upper The increasing direction of is the positive direction, so the value corresponding to the coordinate value=topBase+x;

For the right side, x is unchanged, and y is changing, that is, when x=2 (x=c), 11=25-14, so the right base value rightBase=MaxValue-7*x, because of the increasing direction on the right It is the positive direction, so the value corresponding to the coordinate value=rightBase+y; for the bottom, y is unchanged, and x is changing, that is, when y=2 (y=c), 15=25-10, so the base The value bottomBase=MaxValue-5*y, because the growth direction below is negative, so value=bottomBase-x;

For the left side, x is unchanged, y is changing, that is, when x=-2(x==-c), 19=25-6, so the left base value leftBase=MaxValue+3*x; the left side grows The direction is negative, so value=leftBase-x;

Code:

int max(int a,int b){
  return a>b?a:b;
}
int abs(int a){
 return a>0?a:-a;
}
int test(int x,int y){
 int c=max(abs(x),abs(y));//取x,y绝对值中的最大值;圈数
 int maxValue=(2*c+1)*(2*c+1);
 //上边
if(y==-c){
 return maxValue+y+x;
}
//右边
else if(x==c){
return maxValue-7*x+y);
}
//下边
else if(y==c){
return maxValue-5*y-x);
}
//左边
else{
return maxValue+3*x-y;
}
}
void main(){
test(2,-2);//17;
//打印整个矩阵
//顺时针
int i,j;
for(i=-4;i<4;i++){
 for(j=-4;j<4;j++){
  printf("%5d",test(j,i));
}
}
//如果想要逆时针的螺旋矩阵,交换i.j的位置即可
for(i=-4;i<4;i++){
 for(j=-4;j<4;j++){
  printf("%5d",test(i,j));
}
}
}

[Extension]:

Print the following matrix A:

 

1  2  3   4  5
16 17 18 19  6 
15 24 25 20  7
14 23 22 21  8
13 12 11 10  9

In fact, the idea is similar, clockwise, it is divided into several circles and several circles, the first circle is from 1 to 16, the second circle is from 17 to 24, and then study it carefully:

Take the outermost circle to study, the coordinates (a, b),

int m=1, i=0,j;

Above, from 1 to 5, a does not change, b changes
for (j=0;j<5;j++) A[i][j]=m++;

On the right, starting from 6 to 8, a changes, b does not change, b=4

for(j=i+1;j<4;j++) A[j][4]=m++;

Below, from 10 to 12, a does not change, b changes, a=4

for(j=n-i-1;j>i;j--)A[n-i-1][j]=m++;

On the left, from 13 to 16, a changes, b does not change, b=i

for(j=n-i-1;j>i;j--)A[j][i]=m++;

Now it is a 5*5 matrix, so there are 5/2 cycles,

So we have to loop 5/2 times


int m =1 ;
for(i=0;i<n/2;i++){
  for(j=0;j<n;j++)A[i][j] = m++; 
  for(j=i+1;j<n-1;j++)A[j][n-i-1]=m++;
  for(j=n-i-1;j>i;j--)A[n-i-1][j]=m++;
  for(j=n-i-1;j>i;j--)A[j][i]=m++;}
if(n%2==1){
A[n/2][n/2]=n*n
}


Guess you like

Origin blog.csdn.net/sinat_35803474/article/details/76975256