You are given a standard chessboard consisting of 64 squares (eight rows and eight columns) arranged in two alternating colors. There are no pieces on the board except for one knight. The knight moves according to standard chess rules.
It can move two squares horizontally and one square vertically, or two squares vertically and one square horizontally. The completed move therefore looks like the letter 'L'. The green dots on the image represent all eight possible moves.
The task is to write a function based on the input of the beginning and end positions, and will return a minimum number of moves required for the knight to reach the end, indicated by a checkmark on the chessboard:
int FindPath(int x, int y, int x1, int y1)
It can move two squares horizontally and one square vertically, or two squares vertically and one square horizontally. The completed move therefore looks like the letter 'L'. The green dots on the image represent all eight possible moves.
The task is to write a function based on the input of the beginning and end positions, and will return a minimum number of moves required for the knight to reach the end, indicated by a checkmark on the chessboard:
int FindPath(int x, int y, int x1, int y1)
initialize an 8x8 integer array with -1 // represents the chessboard
put a zero at x, y // the knight's initial position
while (the array element at x1, y1 is -1) do // the knight's final position
for each element of the array
if that element is not -1
remember that element's value+1
for each of the eight move directions
if (the resulting position is in bounds) then
if (the resulting position's value is -1) then
set the resulting position's value to the remembered value
return the value of the array element at x1, y1