Skip to content

Instantly share code, notes, and snippets.

@kupp1
Created March 26, 2019 15:43
Show Gist options
  • Select an option

  • Save kupp1/88ccefac01b54e15c35412838cf5ed61 to your computer and use it in GitHub Desktop.

Select an option

Save kupp1/88ccefac01b54e15c35412838cf5ed61 to your computer and use it in GitHub Desktop.
Square spiral matrix on Common Lisp
(defun spiral-matrix (mat n &optional (x 0) (y 0) (k 1) (dx 1) (dy 0) (defval 0))
;; Рекурсивная функция для создания спиральной квадратной матрицы (по часовой стрелке)
;; mat - массив n*n заполненый изчально defval
;; никаких проверок нет, если mat или n кривые будет ошибка
(if (<= k (* n n))
(progn
(setf (aref mat y x) k)
(let ((x1 (+ x dx)) (y1 (+ y dy)))
(if (and
(>= x1 0)
(< x1 n)
(>= y1 0)
(< y1 n)
(= (aref mat y1 x1) defval))
(spiral-matrix mat n x1 y1 (1+ k) dx dy)
(spiral-matrix mat n (+ x (- dy)) (+ y dx) (1+ k) (- dy) dx))))))
(defvar *n* (read))
(defvar *mat* (make-array (list *n* *n*) :initial-element 0))
(spiral-matrix *mat* *n*)
(defvar *frmt* (concatenate 'string "~" (write-to-string (length (write-to-string (* *n* *n*)))) "d "))
(do ((y 0 (1+ y))) ((> y (1- *n*)) y)
(do ((x 0 (1+ x))) ((> x (1- *n*)) x)
(format t *frmt* (aref *mat* y x)))
(terpri))
(exit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment