# Minimum Knight Moves

## Problem

![](/files/-MRL_RSyAL5K2TrFEdK7)

![](/files/-MRL_XQJKA7xcNHXIVun)

### Thought Process

* We care about the levels in this problem

* Infinite chess board meaning no boundaries&#x20;

* BFS to look at all the neighbors&#x20;

![](/files/-MRL_xzE05MWEZjeBK-q)

## Solution

```
from collections import deque

class Solution:
    def minKnightMoves(self, x: int, y: int) -> int:
        q = deque()
        visited = set((0,0))
        q.append([0,0])
        steps = 0
        direc = [[-2,-1], [-2,1],[2,-1],[2,1],[-1,-2], [1,-2], [-1,2], [1,2]]
        x = abs(x)
        y = abs(y)
        
        while q:
            levelSize = len(q)
            for _ in range(levelSize):
                coor = q.popleft()
                xCoor = coor[0]
                yCoor = coor[1]
                
                if xCoor == x and yCoor == y:
                    return steps
                
                else:
                    for i in direc:
                        newX = xCoor + i[0]
                        newY = yCoor + i[1]
                
                        if (newX, newY) not in visited and (newX>=-2 and newY >= -2):
                
                            q.append([newX,newY])
                            visited.add((newX, newY))
            steps+=1 
            
```

## Time Complexity:

* **Time**: O(n) where n is the number of moves we have to make
* **Space:** O(n) where n is the number of moves we have to make


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://joshualbarb.gitbook.io/leetcode-problems/graphs/minimum-knight-moves.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
