螺旋队列的样子如下图:
两大规律:
1。螺旋规律(红线)
2。奇数平方规律(紫线)
问题描述:
设1的坐标是(0,0),的方向向右为正,y方向向下为正,例如,7的坐标为(-1,-1),
2的坐标为(0,1)。编程实现输入任意一点坐标(x,y),输出所对应的数字!
问题解决:
从紫线突破。从图中不难发现,右上角vc=(2*t+1)(2*t+1),t为该圈x,y坐标的绝对值的最大值。例如vc=9、25、49、81........,算出vc后,就分4个判断区域,分别判断,点落在该圈4条边的哪条边上,从而计算出具体坐标点的值。
四个区域划分如下图:
4个区域内4条边上的值u与vc的对应关系为:
y=-t区:u = vc+(x+y);
x=-t区:u = vc+(3*x-y);
y=t区:u = vc + (-x - 5*y);
x=t区:u = vc+(-7*x+y);
那么这些关系是怎么得出来的呢?再看图中画上圈的数字:
在y=-t区,y坐标不变,x坐标变化步长为1。令x=0,此时,u=vc+y作为该边的基准值,其他值随x的变化而变化,得在该区域u=vc+(x+y);
同理,在x=-t区,x坐标不变,y坐标变化步长为1。令y=0,此时,u=vc+3*x作为该边的基准值,其他值随y的变化而变化,得在该区域u=vc+(3*x-y);
同理得其他俩区域的表达式。不再赘述。
程序实现:
- #include <iostream>
- #include <string>
- using namespace std;
- #define abs(a) ((a)>0?(a):(-a))
- #define max(a,b) ((a)>(b)?(a):(b))
- int spiralval(int x,int y)
- {
- int t = max(abs(x),abs(y));
- int vc = (t*2+1)*(t*2+1);
- int u;
- if ( y == -t) //分别判断,点落在该圈4条边的哪条边上
- u = vc+(x+y);
- else if (x == -t)
- u = vc+(3*x-y);
- else if (y == t)
- u = vc + (-x - 5*y);
- else
- u = vc+(-7*x+y);
- return u;
- }
- int main()
- {
- int x,y;
- cout << endl;
- for(y=-5;y<=5;y++)
- {
- for(x=-5;x<=5;x++)
- printf("%5d",spiralval(x,y));
- printf("/n");
- }
- cin.get();
- return 0;
- }
一种螺旋队列的描述:
21 22 23 24 。。
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
这是一个顺时针的螺旋数列,设1的坐标是(0,0),取x的正方向为向右,取y的正方向为向下。则7的坐标就是(-1,-1),2的坐标是(0,1),3的坐标是(1,1)。
现在需要编程实现:
1,输入一个坐标输出对应的数字;
2,输入一个数字输出对应的坐标。
代码如下:
- #include <stdio.h>
- int step=0;
- int key=1;
- int N=0;
- typedef struct
- {
- int x;
- int y;
- }node;
- node A[100];
- int temp_x=0,temp_y=0;
- int x,y=0;
- bool Isexsit();
- bool Isaim();
- int main()
- {
- printf("1.输入坐标->输出对应的数(默认)/n2.输入数字->输出对应的坐标/n");
- input:
- printf("请选择模式:");
- scanf("%d",&key);
- if(key==1)
- {
- printf("输入坐标值:");
- scanf("%d%d",&x,&y);
- }
- else if(key==2)
- {
- printf("输入数字:");
- scanf("%d",&N);
- }
- else
- {
- printf("Error Operation/n");
- goto input;
- }
- while(1)
- {
- operation1:
- temp_x++;step++;
- if(!Isexsit())
- {
- A[step].x=temp_x;
- A[step].y=temp_y;
- if(Isaim())
- break;
- }
- else if(Isexsit())
- {
- temp_x--;step--;
- goto operation4;
- }
- operation2:
- temp_y++;step++;
- if(!Isexsit())
- {
- A[step].x=temp_x;
- A[step].y=temp_y;
- if(Isaim())
- break;
- }
- else if(Isexsit())
- {
- temp_y--;step--;
- goto operation1;
- }
- operation3:
- temp_x--;step++;
- if(!Isexsit())
- {
- A[step].x=temp_x;
- A[step].y=temp_y;
- if(Isaim())
- break;
- }
- else if(Isexsit())
- {
- temp_x++;step--;
- goto operation2;
- }
- operation4:
- temp_y--;step++;
- if(!Isexsit())
- {
- A[step].x=temp_x;
- A[step].y=temp_y;
- if(Isaim())
- break;
- }
- else if(Isexsit())
- {
- temp_y++;step--;
- goto operation3;
- }
- }
- if(key==1)
- printf("%d/n",step+1);
- else
- printf("%d %d/n",temp_x,temp_y);
- return 0;
- }
- bool Isexsit()
- {
- int i=0;
- while(i<50)
- {
- if(A[i].x==temp_x&&A[i].y==temp_y)
- return true;
- i++;
- }
- return false;
- }
- bool Isaim()
- {
- if(key==1)
- {
- if(temp_x==x&&temp_y==y)
- return true;
- else return false;
- }
- else //key==2
- {
- if(step+1==N)
- return true;
- else return false;
- }
- }
运行结果如下:
欢迎各路牛人批评指正~