The chess knight should not step on the same ground.

1 view (last 30 days)
For a knight in square E-8 on a chessboard;
Write the code that finds the sequence of moves in which all the frames are touch without going through a square again.
You can think of the chessboard [8] [8] as a 2-dimensional array.
You can show the E8 square as [1] [8].
Can you help me pls
Ty.

Answers (1)

Walter Roberson
Walter Roberson on 10 Feb 2021
Have a fixed order in which you try next moves.
Each attempt, look at the value in your current square, and index that in to the information about where to try next (row and column offsets). Add those offsets to your current position and see if the new position is on the board, and if the array is 0 at that location. If it is, then write that new location at the end of your list of moves and write 1 into the board at that location and loop back. If it is not on the board or the board is not 0 there, check to see if the current location is already at the last defined strategy; if not (there are more available) add 1 to the current location and loop back. If there are no more strategies available for the current location, then remove the location from the end of the list of moves, and set the position on the board to 0, and go back to the position that is now on the end of the list, check to see if it is exhausted, if not add 1 and loop back, otherwise regress another step and so on.
The above sequence can be tuned a bit to prevent redundant checks.

Categories

Find more on Board games in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!