1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Nクイーン高速化

Posted at
fast.scm
#!/usr/local/bin/gosh

(define (solve? num ls)
  (let loop ((ls ls)(n 1))
	(cond
	  ((null? ls)#t)
	  ((= (abs (- num (car ls))) n)
	   #f)
	  (else (loop (cdr ls)(+ n 1))))))

(define (check? lst)
  (let loop ((lst lst))
	(cond
	  ((null? (cdr lst))#t)
	  ((solve? (car lst) (cdr lst))
	   (loop (cdr lst)))
	  (else #f))))

(define (queen lst)
  (let loop ((lst lst)(result '()))
	(if (null? lst)
	  (print (reverse result))
	  (for-each (lambda(x)
		(if (check? (reverse (cons x result)))
		          (loop (delete x lst) (cons x result)))) lst))))

;(time (queen '(0 1 2 3 4 5 6 7)))
; real 0.008
; user 0.010
; sys 0.000

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?