AcWing 1237. 螺旋折线
原题链接
中等
作者:
fedfan
,
2020-07-08 22:13:00
,
所有人可见
,
阅读 787
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int ans;
ll sum(ll n)
{
return 1 + 4 * n * n + 4 * n;
}
ll dis(ll x, ll y)
{
if(x == 0 && y == 0) return 0;
if(x < 0)
{
if(y <= -x && y >= x + 1)
{
return sum(-x - 1) + y - (x + 1);
}
else if(y > -x)
{
return sum(y - 1) + 2 * y - 1 + x - (-y);
}
else
{
ans++;
return sum(-y - 1) + 2 * (-y) - 1 + 2 * (-y) * 2 + (-y) - x;
}
}
else
{
if(y <= x && y >= -x)
{
return sum(x - 1) + 2 * x - 1 + 2 * x + x - y;
}
else if(y > x)
{
return sum(y - 1) + 2 * y - 1 + x + y;
}
else
{
return sum(-y - 1) + 2 * (-y) - 1 + 2 * (-y) * 2 + (-y) - x;
}
}
}
int main()
{
ll x, y;
cin >> x >> y;
cout << dis(x, y) << endl;
return 0;
}