TopCoder problem "Pathfinding" used in SRM 345 (Division I Level One , Division II Level Two)



Problem Statement

    

Recently you have been working on the pathfinding module for your AI system. Your objective is to find the shortest path for an agent that wants to travel between two points on a plane. The agent will start at the point (0,0), and travel to the point (x,y). You decided that the agent will move either on horizontal of vertical lines such that, at every moment, at least one coordinate of the agent is an integer.

There is yet another restriction however. Each line will only allow movement in one direction. All horizontal lines with odd y-coordinates will be directed toward decreasing values of x, and all other horizontal lines toward increasing values of x. Similarly, all vertical lines with odd x-coordinates will be directed toward decreasing values of y, and all other vertical lines toward increasing values of y.

Given x and y, return the distance that the agent must travel.

 

Definition

    
Class:Pathfinding
Method:getDirections
Parameters:int, int
Returns:int
Method signature:int getDirections(int x, int y)
(be sure your method is public)
    
 

Constraints

-x and y will both be between -10^6 and 10^6, inclusive.
 

Examples

0)
    
0
-4
Returns: 8
A possible path from (0,0) to (0,-4) is through the points (1,0), (1,-3), (-1,-3) and (-1,-4).
1)
    
5
-4
Returns: 9
2)
    
5
4
Returns: 9
3)
    
-1
-4
Returns: 7
4)
    
0
0
Returns: 0

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7649

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10669&pm=7649

Writer:

rusolis

Testers:

PabloGilberto , brett1479 , Olexiy , ged

Problem categories:

Geometry, Simple Math