Title:
Spiral Polyline
The spiral polyline as shown in figure p1.png passes through all the whole points on the plane exactly once.
For the whole point (X, Y), we define its distance to the origin dis(X, Y) is the length of the helical polyline segment from the origin to (X, Y).
For example dis(0, 1)=3, dis(-2, -1)=9
gives integer coordinates (X, Y), can you calculate dis(X, Y)?
fun(-1,0,n,m);
The spiral polyline as shown in figure p1.png passes through all the whole points on the plane exactly once.
For the whole point (X, Y), we define its distance to the origin dis(X, Y) is the length of the helical polyline segment from the origin to (X, Y).
For example dis(0, 1)=3, dis(-2, -1)=9
gives integer coordinates (X, Y), can you calculate dis(X, Y)?
【Input format】
X and Y
【Output format】
Output dis(X, Y)
【Sample input】
0 1
【Sample output】
3
Code:
#include<iostream>
#include<math.h>
using namespace std;
int sum=1;
int fun(int x,int y,int n,int m)
{
if(n==0&&m==0)
{
cout<<"0"<<endl;
}
else
{
while(x<=abs(n)+1||y<=abs(m)+1)
{
if(y<0 && -y>=x && x+y!=1 && x-y>=0)//
{
if(x==n && y==m)
{
cout<<sum<<endl;
break;
return 0;
}
x=x-1;
sum++;
}
if(x<0 && -x>y && x-y<=-1)
{
if(x==n && y==m)
{
cout<<sum<<endl;
break;
return 0;
}
x=x+1;
sum++;
}
if(x>0 && y>-x && y-x!=1 && x-y>=0)
{
if(x==n && y==m)
{
cout<<sum<<endl;
break;
return 0;
}
y=y-1;
sum++;
}
}
}
}
int main(int argc, char*argv[]
{
int n,m;
cin>>n>>m;fun(-1,0,n,m);
return 0;
}