> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Knight Moves Python Solution

> Tested Python solution for LeetCode 1197 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 1197, Medium. Topics: Breadth-First Search. [View on LeetCode](https://leetcode.com/problems/minimum-knight-moves/description/).

Generate this problem as a practice environment: tested reference solution, 19 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1197   # by problem number
lcpy gen -s minimum_knight_moves   # by problem name
```

## Problem

In an **infinite** chess board with coordinates from `-infinity` to `+infinity`, you have a **knight** at square `[0, 0]`.

A knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.

Return *the minimum number of steps needed to move the knight to the square* `[x, y]`. It is guaranteed the answer exists.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2018/10/12/knight.png)

```
Input: x = 2, y = 1
Output: 1
Explanation: [0, 0] → [2, 1]
```

```
Input: x = 5, y = 5
Output: 4
Explanation: [0, 0] → [2, 1] → [4, 2] → [3, 4] → [5, 5]
```

### Constraints

* `-300 <= x, y <= 300`
* `0 <= |x| + |y| <= 300`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_knight_moves/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_knight_moves/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque


class Solution:
    # Time: O(x * y)
    # Space: O(x * y)
    def min_knight_moves(self, x: int, y: int) -> int:
        # Symmetry: target mirrored into first quadrant. BFS bounded to a small
        # region around origin + target; allow slight negatives to reach (1,1).
        x, y = abs(x), abs(y)
        seen: set[tuple[int, int]] = {(0, 0)}
        queue: deque[tuple[int, int, int]] = deque([(0, 0, 0)])
        directions = [
            (1, 2),
            (2, 1),
            (-1, 2),
            (-2, 1),
            (1, -2),
            (2, -1),
            (-1, -2),
            (-2, -1),
        ]
        while queue:
            cur_x, cur_y, dist = queue.popleft()
            if cur_x == x and cur_y == y:
                return dist
            for dx, dy in directions:
                nx, ny = cur_x + dx, cur_y + dy
                if (nx, ny) not in seen and -2 <= nx <= x + 4 and -2 <= ny <= y + 4:
                    seen.add((nx, ny))
                    queue.append((nx, ny, dist + 1))
        return -1
```

## Complexity

| Time      | Space     |
| --------- | --------- |
| O(x \* y) | O(x \* y) |

## Tags

[Grind](/catalog/grind), [NeetCode All](/catalog/neetcode).
