Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1,  - 1)], [( - 1,  - 1), (2,  - 1)],[(2,  - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.

Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).

Input

The first line contains two space-separated integers x and y(|x|, |y| ≤ 100).

Output

Print a single integer, showing how many times Valera has to turn.

Sample Input

Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3

题意:给出一个从原点开始螺旋前进的移动规则,现要到达有限范围内的某个坐标,问一共需要几次转向。
模拟。
思路:画一下图找下规律就OK了,开始我以为输入的点只是该点所在正方形上的顶点,其实它是该正方形上任意一个整数点。
 #include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std; int main()
{
int x,y;
while(~scanf("%d %d",&x,&y))
{
if(x == && y == )
{
printf("0\n");
continue;
}
int k = max(abs(x),abs(y));
int ans = (k-)*;
if(x >= k- && x <= k && y == -k)
printf("%d\n",ans);
else if(x == k && y >= -k && y <= k)
printf("%d\n",ans+);
else if(x >= -k && x <= k && y == k)
printf("%d\n",ans+);
else if(x == -k && y >= -k && y <= k)
printf("%d\n",ans+);
else if(x >= -k && x <= k && y == -k)
printf("%d\n",ans+);
}
return ;
}

C - Point on Spira的更多相关文章

  1. 【HDOJ】3832 Earth Hour

    其实就是bfs,不过也可以写成最短路,因为权重为1,可以用Spira解. /* 3832 */ #include <iostream> #include <string> #i ...

  2. 【SSSP】A forward-backward single-source paths algorithm

    0. 引子基础的算法和数据结构已经学习的差不多了,上学期期末就打算重点研究研究STOC和FOCS上面的论文.做这件事情的初衷是了解别人是如何改进原有算法的,搞清楚目前比较热的算法问题有哪些,更重要的是 ...

  3. 英语阅读——A meaningful life

    这篇文章是<新视野大学英语>第四册的第八单元的文章. 1 The death of an angel of animal rights activism(活动家) does not rat ...

随机推荐

  1. Android-关于屏幕适配的一些经验

    刚开始,我开发时选取的模拟器是WVGA854,其分辨率为854*480.我开发完毕后装在800*480的手机上时感觉很OK,但是装到480*320.以及320*240分辨率上的手机时,很多界面都变形了 ...

  2. mysql命令使用

    1.连接Mysql 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL.首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root ...

  3. (转)PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数

    一.中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $l ...

  4. UITableView中容易忽略的知识点

    1.取消余下的分割线 tableView.tableFooterView = UIView() 2.分割线顶格 override func viewDidLayoutSubviews() { self ...

  5. BearSkill实用方法之UITextField限制输入的字符数量

    原文:http://blog.csdn.net/xiongbaoxr/article/details/51525061      

  6. 网页快照 - C#实现

    /// <summary> /// 图片类型枚举 /// </summary> public enum ImageType { GIF = , JPG = , PNG = } ...

  7. 数据挖掘学习笔记:挖掘频繁模式、关联和相关[ZZ]

    所 谓挖掘频繁模式,关联和相关,即指在出现的数据集中找到一个经常出现的序列模式或者是一个经常出现的数据结构.就像搞CPU设计的人知道,Cache的预 取机制有流预取和指针预取,前者就是发现流模式,即发 ...

  8. windows注册表的基本使用——示例

    上网找好多资料发现一个问题就是太老.例如只有RegCreateKey而没有RegCreateKeyEx用法详解,自己摸索了几个小时终于基本用到的几个函数都试用了一遍. 下面代码已经通过编译测试 // ...

  9. 实现 winform 异步跨线程访问UI控件

    在开发winform时经常会用到多线程防止界面出现假死现象,比如当你单击某个按钮时,需要执行很多代码,但是在执行过程中想实时的将当前执行的情况报告给用户,类型进度条或文本什么的. 这个时候很显然,如果 ...

  10. HDU 2993 MAX Average Problem dp斜率优化

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...