#P8668. Spiral Polyline Distance
Spiral Polyline Distance
Spiral Polyline Distance
Consider an infinite spiral polyline that passes through every lattice point (integer coordinates) in the plane exactly once. The spiral is constructed as follows:
Starting at the origin (0,0), the spiral moves in segments with lengths increasing in the following pattern: first left by 1, then up by 1, then right by 2, then down by 2, then left by 3, then up by 3, then right by 4, then down by 4, and so on. In other words, the direction cycle is left, up, right, down with the i-th segment having length (\ell = \lceil i/2\rceil).
For any lattice point ((X, Y)), define (\text{dis}(X, Y)) to be the number of unit steps along the spiral from the origin ((0,0)) to ((X,Y)). For example, note that by this construction:
[
\text{dis}(0,1)=3\quad \text{and} \quad \text{dis}(-2,-1)=9.
]
Your task is: given (X) and (Y), compute (\text{dis}(X, Y)).
inputFormat
The input consists of two space-separated integers (X) and (Y) representing the coordinates of the lattice point.
outputFormat
Output a single integer representing (\text{dis}(X, Y)), i.e. the number of unit steps along the spiral from ((0,0)) to ((X,Y)).
sample
0 1
3