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. Java-hibernate的Hello World

     hibernate 是对jdbc进行轻量级封装的  orm 框架,充当项目的持久层. 要使用 hibernate首先就需要继续配置, 引包:下载hibernate然后加入jar包 同时引入mysql ...

  2. Browser Link: Failed to deserialize JSON in Browser Link call

    问题 VS2013中调试程序发现,在浏览器控制台输出如下截图代码:

  3. 使用linq语句获取指定条数的记录

    //获得指定个数的子文件夹,用于分页 var pageAlbums = (from SPFolder pf in lstSubAlbums select pf)                     ...

  4. 初学HTML5系列三:事件

    Window 事件属性 针对 window 对象触发的事件(应用到 <body> 标签): 属性 值 描述 onafterprint script 文档打印之后运行的脚本. onbefor ...

  5. 域名解析-delphi 源码

    unit Unit1; interface uses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...

  6. 前端--json数据的处理及相关兼容问题

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...

  7. html 中设置span的width完美解决方法

    在默认情况下,由于span是行标签,设置width是无效的.只有改变display的属性,才可以实现设置宽度. 1.初步想法 span{ background-color:#ccc; display: ...

  8. Android简单例子——IpHone样式AlertDialog

    此例子源于网络,下载下来之后,自己加了写注释,作为总结,发到博客中,谢谢原作者 通过这个例子学到的东西 1.自定义对话框的使用 2.程序中使用颜色如何进行存放,增加复用性 3.加深线性布局.常用控件的 ...

  9. angularjs跨域调取webservice

    1.配置 web.config <webServices> <!--必须添加--> <protocols> <add name="HttpGet&q ...

  10. 学 Android 是一种什么样的体验?

    转眼间 2016 年的电量已不足 20%,不禁感慨 How Time Flies!不知不觉 Android 移动开发已经走过了八年的光阴,在这八年的时间中,Android 开发从最初的简单调用系统 A ...