螺旋队列(p98)

  先判断这个坐标代表的数位于哪一层,然后依据该层最大的数去计算这个坐标所代表的数。

 1 #include"iostream"
 2 #include"stdio.h"
 3 #include"algorithm"
 4 #include"map"
 5 using namespace std;
 6 
 7 typedef pair<int,int> pInt2;
 8 
 9 int GetLayer(int x,int y)
10 {
11     return abs(x)>abs(y)?abs(x):abs(y);
12 }
13 
14 int GetRes(int x,int y,int layer)
15 {
16     int res,maxLayerNum=(2*layer+1)*(2*layer+1);
17     if(y==-layer)
18     {
19         if(x>0)
20             res=maxLayerNum-(layer-x);
21         else
22             res=maxLayerNum-layer+x;
23     }
24     else if(x==-layer)
25     {
26         if(y<0)
27             res=maxLayerNum-2*layer-(layer+y);
28         else
29             res=maxLayerNum-3*layer-y;
30     }
31     else if(y==layer)
32     {
33         if(x<0)
34             res=maxLayerNum-4*layer-(layer+x);
35         else
36             res=maxLayerNum-5*layer-x;
37     }
38     else
39     {
40          if(y>0)
41             res=maxLayerNum-6*layer-(layer-y);
42         else
43             res=maxLayerNum-7*layer+y;
44     }
45 }
46 
47 int main()
48 {
49     int x,y;
50     while(cin>>x>>y)
51     {
52         cout<<"(x,y): "<<GetRes(x,y,GetLayer(x,y))<<endl;
53     }
54     return 0;
55 }
View Code
原文地址:https://www.cnblogs.com/acm-jing/p/10240085.html