How to resolve the algorithm Knight's tour step by step in the PicoLisp programming language
How to resolve the algorithm Knight's tour step by step in the PicoLisp programming language
Table of Contents
Problem Statement
Problem: you have a standard 8x8 chessboard, empty but for a single knight on some square. Your task is to emit a series of legal knight moves that result in the knight visiting every square on the chessboard exactly once. Note that it is not a requirement that the tour be "closed"; that is, the knight need not end within a single move of its start position. Input and output may be textual or graphical, according to the conventions of the programming environment. If textual, squares should be indicated in algebraic notation. The output should indicate the order in which the knight visits the squares, starting with the initial position. The form of the output may be a diagram of the board with the squares numbered according to visitation sequence, or a textual list of algebraic coordinates in order, or even an actual animation of the knight moving around the chessboard. Input: starting square Output: move sequence
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Knight's tour step by step in the PicoLisp programming language
Source code in the picolisp programming language
(load "@lib/simul.l")
# Build board
(grid 8 8)
# Generate legal moves for a given position
(de moves (Tour)
(extract
'((Jump)
(let? Pos (Jump (car Tour))
(unless (memq Pos Tour)
Pos ) ) )
(quote # (taken from "games/chess.l")
((This) (: 0 1 1 0 -1 1 0 -1 1)) # South Southwest
((This) (: 0 1 1 0 -1 1 0 1 1)) # West Southwest
((This) (: 0 1 1 0 -1 -1 0 1 1)) # West Northwest
((This) (: 0 1 1 0 -1 -1 0 -1 -1)) # North Northwest
((This) (: 0 1 -1 0 -1 -1 0 -1 -1)) # North Northeast
((This) (: 0 1 -1 0 -1 -1 0 1 -1)) # East Northeast
((This) (: 0 1 -1 0 -1 1 0 1 -1)) # East Southeast
((This) (: 0 1 -1 0 -1 1 0 -1 1)) ) ) ) # South Southeast
# Build a list of moves, using Warnsdorff’s algorithm
(let Tour '(b1) # Start at b1
(while
(mini
'((P) (length (moves (cons P Tour))))
(moves Tour) )
(push 'Tour @) )
(flip Tour) )
You may also check:How to resolve the algorithm Factors of a Mersenne number step by step in the Maxima programming language
You may also check:How to resolve the algorithm Stack step by step in the Swift programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the AWK programming language
You may also check:How to resolve the algorithm Gray code step by step in the Wren programming language
You may also check:How to resolve the algorithm Teacup rim text step by step in the Haskell programming language