Created
March 26, 2019 15:43
-
-
Save kupp1/88ccefac01b54e15c35412838cf5ed61 to your computer and use it in GitHub Desktop.
Square spiral matrix on Common Lisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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