# Solution For Minimum Knight Moves

The Minimum Knight Moves problem on Leetcode asks us to find the minimum number of steps a chess knight needs to take in order to move from the origin (0,0) to a given target location (x,y) on an 8×8 chessboard. The knight can move two squares in one direction and one square in the other, either vertically or horizontally.

To solve this problem, we can use a breadth-first search algorithm. We will start from the origin, and we will enqueue all possible moves that the knight can make from the current position. We will keep track of the number of steps taken to reach that position and mark it as visited to avoid revisiting it again. We will continue this process until we reach the target position or visit all possible positions on the board.

Here is the Python code for the Minimum Knight Moves problem on Leetcode:

“`
from collections import deque

def minKnightMoves(x: int, y: int) -> int:
# Define the possible moves of a knight
moves = [(-2,-1),(-2,1),(2,-1),(2,1),(-1,-2),(-1,2),(1,-2),(1,2)] # Initialize the queue with the origin and mark it as visited
queue = deque([(0,0,0)])
visited = {(0,0)}
while queue:
# Dequeue the next position and number of steps taken to reach that position
curr_x, curr_y, curr_steps = queue.popleft()
if curr_x == x and curr_y == y:
# We have reached the target position, return the number of steps taken
return curr_steps
# Enqueue all possible moves
for dx, dy in moves:
next_x, next_y = curr_x + dx, curr_y + dy
if (next_x,next_y) not in visited and -2 <= next_x <= x+2 and -2 <= next_y <= y+2:
# Add the next position and number of steps taken to reach that position to the queue
queue.append((next_x, next_y, curr_steps+1))
“`

In this solution, we use a set called “visited” to keep track of the positions that have been visited. We also add an extra border of two squares on each side of the board to handle edge cases where the target position is at the edge of the board or outside the board.

The time complexity of this solution is O(xy), where x and y are the coordinates of the target position. The space complexity is also O(xy) to store the visited positions.

## Step by Step Implementation For Minimum Knight Moves

```There are many ways to solve this problem. One approach would be to use a breadth first search algorithm. We can start from the position (0, 0) and explore all of the possible knight moves from that position. We can keep track of the minimum number of moves needed to reach each position and return the minimum number of moves needed to reach the target position.

Another approach would be to use a dynamic programming algorithm. We can create a 2D array where each position represents the minimum number of moves needed to reach that position. We can then fill in the array using a bottom-up approach, starting from the position (0, 0).```
```from collections import deque

# BFS solution
# Time Complexity: O(N^2)
# Space Complexity: O(N^2)

def minKnightMoves(x, y):
# If the target is (0, 0), return 0
if x == 0 and y == 0:
return 0

# Create a visited set to keep track of visited cells
visited = set()

# Create a queue for BFS
queue = deque()

# Add the starting position to the queue
queue.append((0, 0))

# Set the starting distance to 0
distance = 0

# Perform BFS
while queue:
# Get the size of the queue
size = len(queue)

# Iterate over all the nodes in the current level
for _ in range(size):
# Get the front node from the queue
curr_x, curr_y = queue.popleft()

# If the current node is the target, return the distance
if curr_x == x and curr_y == y:
return distance

# If the current node is already visited, skip it
if (curr_x, curr_y) in visited:
continue

# Add the current node to the visited set

# Add the next possible moves from the current node to the queue
queue.append((curr_x + 1, curr_y + 2))
queue.append((curr_x + 2, curr_y + 1))
queue.append((curr_x - 1, curr_y + 2))
queue.append((curr_x + 2, curr_y - 1))
queue.append((curr_x - 2, curr_y + 1))
queue.append((curr_x + 1, curr_y - 2))
queue.append((curr_x - 1, curr_y - 2))
queue.append((curr_x - 2, curr_y - 1))

# Increment the distance by 1 after finishing the current level
distance += 1

# If we reach here, it means that the target is not reachable
return -1```
```There are a few different ways to solve this problem. One way would be to use a Breadth First Search algorithm. Another way would be to use a Dynamic Programming approach.

We will use a Dynamic Programming approach here. We will create an array of size NxN, where N is the size of the board. We will initialize this array with all -1 values. Then, we will start at the position of the knight (x,y). We will set the value of this position to 0. From here, we will explore all of the possible moves the knight can make, and update the array accordingly.

The knight can move in 8 different directions: (2,1), (2,-1), (1,2), (1,-2), (-1,2), (-1,-2), (-2,1), (-2,-1).

We will explore each of these directions in turn. For each direction, we will compute the new position of the knight, and update the array accordingly. If the new position is out of bounds, or if the value of the position is already set (meaning we have already visited this position), we will skip it. Otherwise, we will set the value of the position to the current value of the knight plus one (since it takes one move to get to this position from the knight's current position).

Once we have explored all of the possible moves from the knight's current position, we will repeat this process for the next position in the array. We will continue until we have reached the end of the array.

The value of the knight's final position will be the minimum number of moves required to reach the end of the array.```
```There are many possible solutions to this problem. One approach is to use a breadth-first search algorithm to find the shortest path from the starting position to the destination position. The code for this solution is provided below.

#include
#include
#include
using namespace std;

struct Position {
int x, y;
};

int minimumKnightMoves(int x, int y) {
// Perform a breadth-first search to find the shortest path from
// the starting position to the destination position.
queue q;
unordered_set visited;

// Add the starting position to the queue.
Position start = {0, 0};
q.push(start);
visited.insert(start.x * 100 + start.y);

// Perform the search.
int steps = 0;
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
Position curr = q.front();
q.pop();

// Check if we have reached the destination.
if (curr.x == x && curr.y == y) {
return steps;
}

// Add the next possible positions to the queue.
Position next1 = {curr.x + 2, curr.y + 1};
Position next2 = {curr.x + 2, curr.y - 1};
Position next3 = {curr.x - 2, curr.y + 1};
Position next4 = {curr.x - 2, curr.y - 1};
Position next5 = {curr.x + 1, curr.y + 2};
Position next6 = {curr.x + 1, curr.y - 2};
Position next7 = {curr.x - 1, curr.y + 2};
Position next8 = {curr.x - 1, curr.y - 2};

// Add the next positions to the queue if they have not been visited yet.
if (visited.find(next1.x * 100 + next1.y) == visited.end()) {
q.push(next1);
visited.insert(next1.x * 100 + next1.y);
}
if (visited.find(next2.x * 100 + next2.y) == visited.end()) {
q.push(next2);
visited.insert(next2.x * 100 + next2.y);
}
if (visited.find(next3.x * 100 + next3.y) == visited.end()) {
q.push(next3);
visited.insert(next3.x * 100 + next3.y);
}
if (visited.find(next4.x * 100 + next4.y) == visited.end()) {
q.push(next4);
visited.insert(next4.x * 100 + next4.y);
}
if (visited.find(next5.x * 100 + next5.y) == visited.end()) {
q.push(next5);
visited.insert(next5.x * 100 + next5.y);
}
if (visited.find(next6.x * 100 + next6.y) == visited.end()) {
q.push(next6);
visited.insert(next6.x * 100 + next6.y);
}
if (visited.find(next7.x * 100 + next7.y) == visited.end()) {
q.push(next7);
visited.insert(next7.x * 100 + next7.y);
}
if (visited.find(next8.x * 100 + next8.y) == visited.end()) {
q.push(next8);
visited.insert(next8.x * 100 + next8.y);
}
}
// Increment the number of steps taken.
steps++;
}

// If we reach this point, it means that we have not found a path from
// the starting position to the destination.
return -1;
}

int main() {
cout << minimumKnightMoves(2, 1) << endl;
cout << minimumKnightMoves(5, 5) << endl;
return 0;
}```
```using System;

public class Solution {
public int MinKnightMoves(int x, int y) {
// TODO: Implement solution
}
}```

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]