螺旋折线
这道题属实不易,可以用蛇形矩阵来操作,也可以模拟但限制时间之后都过不了全分
分析性质得出结论:找到4个直角点连线,推出上下左右所有边上的点
还要注意测试数据范围,防止爆int ;时间复杂度:O(1)
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
int main()
{
int x, y;
cin >> x >> y;
if (abs(x) <= y) // 在上方
{
int n = y;
cout << (LL)(2 * n - 1) * (2 * n) + x - (-n) << endl;
}
else if (abs(y) <= x) // 在右方
{
int n = x;
cout << (LL)(2 * n) * (2 * n) + n - y << endl;
}
else if (abs(x) <= abs(y) + 1 && y < 0) // 在下方
{
int n = abs(y);
cout << (LL)(2 * n) * (2 * n + 1) + n - x << endl;
}
else // 在左方
{
int n = abs(x);
cout << (LL)(2 * n - 1) * (2 * n - 1) + y - (-n + 1) << endl;
}
return 0;
}