> For the complete documentation index, see [llms.txt](https://joshualbarb.gitbook.io/leetcode-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://joshualbarb.gitbook.io/leetcode-problems/graphs/minimum-knight-moves.md).

# 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
