Minimum Queen Moves
InterviewTime limit2sMemory limit512 MB
Given a queen's start and target square on an 8x8 board, print the minimum number of queen moves needed.
- Level
Medium4 of 10
- Topics
- Math, Implementation, Geometry
- Solved
- No attempts yet
Problem
A chess queen moves any number of squares at once along its row, along its column, or along one of the two diagonals. The figure below shows the squares a queen reaches in a single move.

The chess grandmaster Kary Gasparov invented a new kind of problem. A single queen stands on an 8 x 8 board with 64 squares and no other piece on it. Given a target square, what is the smallest number of moves the queen needs to get there?
Kary solved some of these cases himself but got stuck on the rest, so write a program that answers them.
Input
The input holds several test cases. Each test case is one line with four integers , , , separated by spaces (). The queen starts on the square with coordinates , and the target is the square with coordinates .
Columns are numbered 1 to 8 from left to right and rows are numbered 1 to 8 from top to bottom. The square in row and column has coordinates .
The last line of the input holds four zeros. Do not process that line.
Output
For each test case print one line with one integer, the smallest number of moves the queen needs to reach its target square.