解题思路
按照题目中的规律画出来的图片如下:
那么,我们直接根据规律判断当前查询的节点在那一条线段上就可以了。易得,当前的基础转向次数为 max ( ∣ x ∣ − 1 , ∣ y ∣ − 1 ) × 4 (|x|-1,|y|-1)\times 4 (∣x∣−1,∣y∣−1)×4,那么加上一个在当前周期内部的转向次数就可以了。
AC 代码
#include<bits/stdc++.h>
#define ll long long
#define ull usigned long long
using namespace std;
const string TypideName="c";
inline void readc(char &c){
c=getchar();
while(c==' '||c=='\n')
c=getchar();
}inline void writec(char c){putchar(c);}
template<typename T>inline void read(T& x) {
if(typeid(x).name()==TypideName){char ch;readc(ch);x=ch;return;}
x = 0; bool f = false; char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-') f = !f; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); }
x = (f ? -x : x); return;
}template<typename T>inline void put(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) put(x / 10);
putchar(x % 10 + '0'); return;
}template<typename T>inline void write(T x) {
if(typeid(x).name()==TypideName){writec(x);return;}
put(x);
}
template<typename T,typename... Args>
inline void read(T& x,Args&...x_) {read(x),read(x_...);}
template<typename T,typename... Args>
inline void write(T x,Args...x_){write(x),write(x_...);}
int x,y;
inline void work(){
read(x,y);
if(x==0&&y==0) {write(0);return;}
int ans=(max(abs(x),abs(y))-1)*4;
if(y<0&&x>y&&x<=-y) ans+=4;
else if(x==max(abs(x),abs(y))&&y>1-max(abs(x),abs(y))&&y<=max(abs(x),abs(y))) ans+=1;
else if(y==max(abs(x),abs(y))&&x>=-max(abs(x),abs(y))&&x<=max(abs(x),abs(y))) ans+=2;
else if(x==-max(abs(x),abs(y))&&y>=-max(abs(x),abs(y))&&y<=max(abs(x),abs(y))) ans+=3;
write(ans,'\n');
}
signed main(){
work();
return 0;
}