Anticlockwise Motion
Time limit1sMemory limit512 MB
Given two cells of an anticlockwise spiral numbered from the center, find the Manhattan distance between them.
- Level
Medium4 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
You have an idea for a board game. The board is a grid of numbered squares. The centre square holds the number 1, and every other number sits on an anticlockwise spiral that grows outwards. The spiral first moves downwards, then to the right, then upwards, then to the left, then downwards again, and so on. The figure below shows the middle 25 squares, and the figure in the hint shows the middle 441 squares.

Figure A.1: the middle 25 squares.
A player moves one square at a time, up, down, left, or right. Given the numbers of two squares, find the smallest number of moves needed to get from one to the other.
Input
The first and only line contains the number of the starting square and the number of the ending square, separated by a space. (, )
Output
Print the shortest distance between square and square , that is the minimum number of moves.
Hint

Figure A.2: the middle 441 squares.