The Knight's Journey
Time limit1sMemory limit128 MB
For each target square (x, y) up to 1e9 away, find the smallest number of knight moves needed to reach it from the origin.
- Level
Medium7 of 10
- Topics
- Math, Greedy, Brute force, Implementation
- Solved
- No attempts yet
Problem
In chess, a knight moves two squares in one direction and one square perpendicular to it: two squares horizontally and one vertically, or one square horizontally and two vertically. On an infinitely large chessboard, a knight standing at can therefore move in a single step to any of , , , , , , , .
Given two integers and , write a program that computes the minimum number of moves a knight needs to travel from to on this infinite board.
Input
The input consists of several test cases. Each test case is a single line containing two integers and separated by a space. The absolute value of each does not exceed one billion ().
The last line of the input contains END, which marks the end of the input.
Output
For each test case, output on its own line the minimum number of moves the knight needs to go from to .