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 ...
随机推荐
- 使用httpModule做权限系统
页面请求过程: 根据这个流程,网上一般的权限验证在:Http.Module.AuthorizeRequestHttp.Module.PreRequestHandlerExecute 例如使用前者: u ...
- 当winform窗体的Bordestyle设置为None时,鼠标可以拖动窗体的办法
方法一: 1 2015-07-11 16:05:35 Point formPoint;//记录窗体的位置 private void Form1_MouseDown(object sender, Mou ...
- java 函数形参传值和传引用的区别
java方法中传值和传引用的问题是个基本问题,但是也有很多人一时弄不清. (一)基本数据类型:传值,方法不会改变实参的值. public class TestFun { public static v ...
- Java-struts2 之值栈问题
这里是根据一个小项目,将数据库的值查出来,然后在页面前台进行遍历的方法 放入值的几种方式: Struts2的三种存值取值的方式 值栈: 栈上下文: ActionContext: package com ...
- python基础知识二
对象 python把在程序中用到的任何东西都成为对象. 每一个东西包括数.字符串甚至函数都是对象. 使用变量时只需要给他们赋一个值.不需要声明或定义数据类型. 逻辑行与物理行 物理行是你在编写程序时所 ...
- JMeter对Selenium自动化代码进行压测
原文转载:http://www.blogjava.net/qileilove/archive/2014/06/05/414423.html 准备工作: 将文件selenium-server-stand ...
- iis7.5 应用程序池 经典模式和集成模式的区别
在 IIS 7.5 中,应用程序池有两种运行模式:集成模式和经典模式. 应用程序池模式会影响服务器处理托管代码请求的方式. 如果托管应用程序在采用集成模式的应用程序池中运行,服务器将使用 IIS 和 ...
- xp和win 2003远程桌面强制进入命令
xp和win 2003远程桌面强制进入命令 注意:端口号也可以不写 如果是在win 2003里面进行踢人的话可以用命令: mstsc /console /v:172.25.100.27:3389 如果 ...
- java中的递归方法
一.含义 递归算法是一种直接或间接地调用自身的算法.在计算机编写程序中,递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于理解. 二.例子 99乘法表的例子 1:普通实现99乘法表太 ...
- openssl移植
一.下载openssl 1.下载网址http://www.openssl.org/source/ 2.下载版本openssl-1.0.0q.tar.gz 二.编译openssl为静态库(X86 lin ...