题目链接:https://cn.vjudge.net/problem/346345/origin
脑残还有救,手残毁一生啊,把*打成+,wa了5发,找了我一个小时的Bug
很巧妙的一个建轴
http://blog.youkuaiyun.com/clz19960630/article/details/50975965
我是看的大牛的博客,不过大牛的博客有错误,在竖直方向上的坐标计算有问题。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
struct node{
int x,y;
};
int rd[20000];
int firstpoint[100],rdnum[100];
int mid1[100],last[100];
void Init(){
int now=1,time=0,sum=0;
for(int i=1;i<=10000;i++){
rd[i]=time;
if(sum==0){
firstpoint[time]=i;
rdnum[time]=now;
mid1[time]=now/2+i;
last[time]=mid1[time]+now/2;
}
sum++;
if(sum==now){
sum=0;
time++;
if(time%2){
now+=2;
}
else
now+=4;
}
}
}
struct node getpoint(int n){
struct node p;
int time=rd[n];
int mid=mid1[time];
p.x=min(abs(n-mid),time);
if(n<mid)
p.x*=-1;
int y0=min(n-firstpoint[time],last[time]-n);
int dn=min((time)/2,y0);
p.y=time%2+2*dn+y0-dn;
return p;
};
int main(){
Init();
int a,b;
while(~scanf("%d%d",&a,&b)){
if(a+b==0){
break;
}
if(a==b){
cout<<"0"<<endl;
continue;
}
struct node point1,point2;
point1=getpoint(a);
point2=getpoint(b);
//cout<<point1.x<<" "<<point1.y<<endl;
//cout<<point2.x<<" "<<point2.y<<endl;
int dx=abs(point1.x-point2.x);
int dy=abs(point1.y-point2.y);
if(dx>=dy)
cout<<max(dx,dy)<<endl;
else{
cout<<dx+(dy-dx)/2<<endl;
}
}
return 0;
}