先上题目:

D - Discover

Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Time Limit: 3000 MS    Memory Limit: 65536 K

Description

 GLC has developed an algorithm to map each lattice point in the 2-d catesian coordinates to a nature number. For example:
(0, 0) -> 0
(1, 0) -> 1
(1, 1) -> 2
(0, 1) -> 3
(-1, 1)-> 4
...
and so on. If we connect every two adjacent points by a segment, we get a spiral line which starts at (0, 0).
Now, given a point on the plane, you are to find the nature number it mapped to.

Input

 The first line of input is the number of test case.
For each test case, thers is only one line contains two number x , y ( |x|, |y| <= 10,000 ).

Output

 for each test case, output the answer in one line.

Sample Input

2
2 3
3 4

Sample Output

31
57   题意:以(0,0)点为起点(零号点),作逆时针回旋矩阵,给出坐标,求出是第几个数。
  直接模拟。根据分析,可以发现想一个方向前进的长度变化是每转两次方向加一。对于要判断目标点是不是在某一段上,我们需要判断这某一段的端点是不是都相等而且等于目标点的其中一个坐标,然后另一个坐标在端点对应坐标之间。不过注意端点的坐标大小不一定是从小到大排的,所以需要分情况讨论。然后统计一下中间的移动步数就可以了。 上代码:
 #include <cstdio>
#include <cstring>
using namespace std; int cy[]={,,,-};
int cx[]={,,-,};
int a,b;
int u;
inline bool check(int l,int m,int r){
if(l<=m && m<=r){
u=m-l;
return ;
}
if(r<=m && m<=l){
u=l-m;
return ;
}
return ;
} int deal(){
int c,i,x,y,x0,y0,ans;
c=,i=;
x=y=;
ans=;
while(){
i++;
for(int j=;j<;j++){
y0=y+cy[c]*i;
x0=x+cx[c]*i;
c=(c+)%;
//printf("%d %d\n",x0,y0);
if(x==x0 && a==x && check(y,b,y0)){
ans+=u;
return ans;
}else if(y==y0 && y0==b && check(x,a,x0)){
ans+=u;
return ans;
}
ans+=i;
y=y0;
x=x0;
}
}
return -;
} int main()
{
int t;
//freopen("data.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d %d",&a,&b);
printf("%d\n",deal());
}
return ;
}

4117

最新文章

  1. SQL Server 中怎么查看一个字母的ascii编码或者Unicode编码
  2. JQuery EasyUI DataGrid根据条件设置表格行样式(背景色)
  3. 过段时间逐步使用HTML5新增的web worker等内容
  4. java GC的优化
  5. 嵌入式环境下通过 UDP 链接来调试 QT 程序
  6. 矿场搭建(codevs 1996)
  7. 传智播客JavaWeb day02笔记
  8. jquery移除、绑定、触发元素事件使用示例详解
  9. OC第一天-Xcode、工程组成及运行状态
  10. ng-src作用
  11. *MySQL卸载之后无法重装,卡在Apply security settings:Error Nr.1045
  12. java-String中的 intern()&lt;转&gt;
  13. 1*Json对象声明简单,复合,对象数组
  14. 转:Web测试需要了解的知识
  15. hdu1356&amp;hdu1944 博弈论的SG值(王道)
  16. Egret学习笔记 (Egret打飞机-6.实现敌机飞起来)
  17. skopeo---github简单记录
  18. 【转】Java Socket编程基础及深入讲解
  19. ListView的BeginUpdate()和EndUpdate()作用[z]
  20. Nancy的基本用法

热门文章

  1. TF101出现“DMClient已停止”处理办法
  2. [RK3288][Android6.0] U-boot 启动流程小结【转】
  3. 2749: [HAOI2012]外星人
  4. poj2342 Anniversary party (树形dp)
  5. Spring:验证用户登录
  6. [BZOJ 1718] Redundant Paths
  7. DCloud-JS-MUI-JS:utils.js
  8. MySQL SQL优化教程
  9. Gold Balanced Lineup(hash)
  10. bzoj1593 [Usaco2008 Feb]Hotel 旅馆(线段树)