I am having some problems generating the correct output for this program. My output is almost correct, but missing of some steps. My code is as follows:
(defun kt (x y m n) ;set the board
(setq totalmoves (* m n)) ;find the total moves
(setq steps 1) ;count steps
(setq lst (list (list x y))) ;list visited with initial points
(findPath x y totalmoves steps m n lst) ;start tour with initial points
)
(defun findPath (x y totalMoves steps m n lst)
(cond
((null lst) NIL)
((= steps totalMoves) lst) ;if steps equals than total moves, then solution is complete
;try to solve the rest recursively
;1- down and right
((canMove (+ x 2) (+ y 1) m n lst)
(findPath (+ x 2) (+ y 1) totalMoves (+ steps 1) m n (appendList (+ x 2) (+ y 1) lst))
)
;2- right and down
((canMove (+ x 1) (+ y 2) m n lst)
(findPath (+ x 1) (+ y 2) totalMoves (+ steps 1) m n (appendList (+ x 1) (+ y 2) lst))
)
;3- right ups
((canMove (- x 1) (+ y 2) m n lst)
(findPath (- x 1) (+ y 2) totalMoves (+ steps 1) m n (appendList(- x 1) (+ y 2) lst))
)
;4- up and right
((canMove(- x 2) (+ y 1) m n lst)
(findPath(- x 2) (+ y 1) totalMoves (+ steps 1) m n (appendList(- x 2) (+ y 1) lst))
)
;5 - up and left
((canMove(- x 2) (- y 1) m n lst)
(findPath(- x 2) (- y 1) totalMoves (+ steps 1) m n (appendList(- x 2) (- y 1) lst))
)
;6- left and up
((canMove(- x 1) (- y 2) m n lst)
(findPath(- x 1) (- y 2) totalMoves (+ steps 1) m n (appendList(- x 1) (- y 2) lst))
)
;7- left and down
((canMove(+ x 1) (- y 2) m n lst)
(findPath(+ x 1) (- y 2) totalMoves (+ steps 1) m n (appendList(+ x 1) (- y 2) lst))
)
;8- down and left
((canMove(+ x 2) (- y 1) m n lst)
(findPath(+ x 2) (- y 1) totalMoves (+ steps 1) m n (appendList(+ x 2) (- y 1) lst))
)
(t
(findPath (car(car(reverse lst))) (car(cdr(car(reverse lst)))) totalMoves steps m n (reverse(cdr (reverse lst))))
)
)
)
(defun appendList (x y lst)
(setq lst (reverse(append (list (list x y)) (reverse lst))))
)
(defun check-visited (x y lst)
(cond
((null lst) 1) ;if nth else to check in list
((equal (list x y) (car lst)) NIL) ;check if curr is visited
((check-visited x y (cdr lst))) ;recurse
)
)
(defun canMove (x y m n lst)
(if (and (<= 1 x) ;for the correct state, all conditions below must be met
(<= x m) ;height is more than or equal to x
(<= 1 y)
(<= y n) ;width is more than or equal to y
(equal (check-visited x y lst) 1)
)
1 NIL ;if all above conds are true, return true else return nil
)
)
The test code is
kt 1 1 5 5
The output is ((1 1) (3 2) (5 3) (4 5) (2 4) (1 2) (3 3) (5 4) (3 5) (1 4) (2 2) (4 3) (5 5) (3 4) (1 5) (2 3) (4 4) (2 5) (1 3) (2 1) (4 2))
There is 21 steps listed here, but it is supposed to have 25.