标签:const Point 279A codeforces Spiral ans return Line Copy
Point on Spiral
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputValera 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).
InputThe first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
OutputPrint a single integer, showing how many times Valera has to turn.
Examples Input Copy0 0Output Copy
0Input Copy
1 0Output Copy
0Input Copy
0 1Output Copy
2Input Copy
-1 -1Output Copy
3
#include<iostream> #include<cstdio> #include<cmath> using namespace std; long long ans = 1, cnt=1; struct Point{ int x,y; Point(){} Point(double a, double b):x(a),y(b){} Point operator-(const Point &a)const{ return Point(x - a.x, y - a.y); } int operator^(const Point &a)const{ return x * a.y - y * a.x; } int operator*(const Point &a)const{ return x * a.x + y * a.y; } void output(){ printf("x = %d y = %d\n", x, y); } }; struct Line{ Point s,e; Line(){} Line(const Point &a, const Point &b):s(a),e(b){} Line(const Line &a){ s = a.s, e = a.e; } bool pointonseg(Point p){ return ((p-s)^(e-s)) == 0 && ((p-s)*(p-e)) <= 0; } void output(){ cout<<"s:"; s.output(); cout<<"e:"; e.output(); cout<<endl; } }; inline void add(Line &a){ if(a.s.x < a.e.x) a = Line(a.e, Point(ans, ans)); else if(a.s.y < a.e.y) a = Line(a.e, Point(-ans, ans)); else if(a.s.x > a.e.x) a = Line(a.e, Point(-ans, -ans)); else { a = Line(a.e, Point(ans + 1, -ans)); ans++; } cnt++; return; } int main(){ int a,b; Line ln(Point(0,0), Point(1,0)); scanf("%d%d",&a, &b); Point d(a,b); while(1){ if(ln.pointonseg(d)){ printf("%I64d", cnt-1); break; } add(ln); } return 0; }
标签:const,Point,279A,codeforces,Spiral,ans,return,Line,Copy 来源: https://www.cnblogs.com/LS-Joze/p/11666553.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。