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. RedHat7 Git 安装使用

    Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势. 搭建git环境 第一步: 安装Git # yum -y install git 第 ...

  2. Sae配置Java数据库连接

    Sae配置Java数据库连接 Sae在Java中配置mysql数据库 >>>>>>>>>>>>>>>>& ...

  3. like的性能问题

    使用like'%匹配的文字%',无法优化,因为索引起不到作用. 不过like'匹配的文字%',索引起作用.

  4. PLSQL Developer操作

    1.设置 1)下载32位Oracle InstantClient  2)将Oracle InstantClient解压到某目录  3)设置环境变量(修改NLS_LANG和TNS_ADMIN环境变量)对 ...

  5. VS编译出现 HTTP 错误 403.14 - Forbidden 决绝办法

    决绝办法:     运行cmd命令,在控制台面板计入Iis Express目录下.运行提示的的就可以了       appcmd set config /section:system.webServe ...

  6. 'EntityValidationErrors' property for more details

    很多小猿遇到这个Exception 的时候,都会有点无厘头.这个时候最好try-- catch下,找到出错的地方.本人习惯在页面上加个lable标签,把exc msg(exception messag ...

  7. autoreleasepool的笔记

    1.autoreleasepool总是会被问到,放在自动释放池中的对象合适被释放?理解不正确的答案:{}出了大括号.出了作用域等等.个人认为参考答案是,1.在不是手动添加的AutoreleasePoo ...

  8. jQuery 杂项方法

    jQuery 杂项方法 方法 描述 data() 向被选元素附加数据,或者从被选元素获取数据 each() 为每个匹配元素执行函数 get() 获取由选择器指定的 DOM 元素 index() 从匹配 ...

  9. [转]操作xml,将xml数据显示到treeview的C#代码

    XmlDocument xml = new XmlDocument(); private void Form1_Load(object sender, EventArgs e) { CreateXML ...

  10. css3学习--css3动画详解一(animation属性)

    ***介绍的属性并不完全,写的都是我认为容易混淆的难点属性,所以属性会在最后综合案例展示~ 一.Keyframes介绍: Keyframes被称为关键帧,其类似于Flash中的关键帧.在CSS3中其主 ...