Minimum Queen Moves to Reach Target Problem
Description
LeetCode Problem 4061.
There is an 8 x 8 empty chessboard with 1-indexed rows and columns.
You are given an array source = [sr, sc] representing the starting position of a queen, and an array target = [tr, tc] representing the target position.
In one move, the queen travels one or more squares along a single row, column, or diagonal, staying within the board.
Return the minimum number of moves for the queen to land exactly on target.
Example 1:
Input: source = [8,1], target = [1,8]
Output: 1
Explanation:

A single diagonal move takes the queen straight from (8, 1) to (1, 8).
Example 2:
Input: source = [4,2], target = [1,3]
Output: 2
Explanation:

The queen moves from (4, 2) to (4, 3), then from (4, 3) to (1, 3), reaching the target in 2 moves.
Example 3:
Input: source = [1,1], target = [1,1]
Output: 0
Explanation:
The queen is already at the target position, so no moves are needed.
Constraints:
source == [sr, sc]target == [tr, tc]1 <= sr, sc, tr, tc <= 8
Solution
Approach: Case Analysis
If the two squares are the same, the answer is 0.
A queen moves any number of squares along a row, a column or a diagonal. If the source and target share a row, a column, or a diagonal (|sr - tr| == |sc - tc|), one move is enough.
Otherwise two moves always work: go from (sr, sc) along the row to (sr, tc), then along the column to (tr, tc). This middle square differs from both ends because they share neither a row nor a column, and the board is empty, so both moves are legal.
Complexity: O(1) time and O(1) space.
class Solution:
def minQueenMoves(self, source: List[int], target: List[int]) -> int:
sr, sc = source
tr, tc = target
if sr == tr and sc == tc:
return 0
if sr == tr or sc == tc or abs(sr - tr) == abs(sc - tc):
return 1
return 2