C - Point on Spira
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
0 0
0
1 0
0
0 1
2
-1 -1
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的更多相关文章
- 【HDOJ】3832 Earth Hour
其实就是bfs,不过也可以写成最短路,因为权重为1,可以用Spira解. /* 3832 */ #include <iostream> #include <string> #i ...
- 【SSSP】A forward-backward single-source paths algorithm
0. 引子基础的算法和数据结构已经学习的差不多了,上学期期末就打算重点研究研究STOC和FOCS上面的论文.做这件事情的初衷是了解别人是如何改进原有算法的,搞清楚目前比较热的算法问题有哪些,更重要的是 ...
- 英语阅读——A meaningful life
这篇文章是<新视野大学英语>第四册的第八单元的文章. 1 The death of an angel of animal rights activism(活动家) does not rat ...
随机推荐
- 使用SBT构建Scala项目
既然决定要在Scala上下功夫,那就要下的彻底.我们入乡随俗,学一下SBT.sbt使用ivy作为库管理工具.ivy默认把library repository建在user home下面. 安装SBT 在 ...
- 使用LuaInterface遇到的编码问题
今天使用LuaInterface加载脚本时忽然报“未知字符”错误信息!于是检查文件编码 将其修改为“US ASCII” 就好了.
- [GDI+] C# ImageClass帮助类教程与源码下载 (转载)
点击下载 ImageClass.rar 功能如下图片 主要功能有:缩略图片,图片水印,文字水印,调整光暗,反色处理,浮雕处理,拉伸处理,左右翻转,上下翻转,压缩图片,图片灰度化,转换为黑白图片,获取图 ...
- wcf入门教程
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
- CSS 列表
CSS列表属性作用如下: 设置不同的列表项标记为有序列表 设置不同的列表项标记为无序列表 设置列表项标记为图像 列表 在HTML中,有两种类型的列表: 无序列表 - 列表项标记用特殊图形(如小黑点.小 ...
- LINQ 101——约束、投影、排序
什么是LINQ:LINQ 是一组 .NET Framework 扩展模块集合,内含语言集成查询.集合以及转换操作.它使用查询的本机语言语法来扩展 C# 和 Visual Basic,并提供利用这些功能 ...
- AngularJS 路由:ui-router
UI-Router是Angular-UI提供的客户端路由框架,它解决了原生的ng-route的很多不足:视图不能嵌套.这意味着$scope会发生不必要的重新载入.这也是我们在Onboard中引入ui- ...
- 为什么用linear regression可以做classification
输出空间 错误衡量方式 能不能直接用linear regression for classification 当成一个分类器回传回去 heuristic(启发式的:试探) 错误衡量 complexit ...
- 实现基于Memcache存储的Session类
自主实现Session功能的类,基于文件方式存储Session数据,测试基本通过,还比较好玩,实际应用没有意义,只不过是学习Session是如何实现的. 使用基于文件的Session存取瓶颈可能都是在 ...
- 彻底删除sql2008r2
一. SQL2008卸载. 1.从控制面板卸载 1)点击计算机右下角“开始”,点击“控制面板” 2)点击“卸载程序”. 3)在程序列表中找到“Microsoft SQL Server 2008” ...