エンジニアとしての市場価値を測りませんか?PR

企業からあなたに合ったオリジナルのスカウトを受け取って、市場価値を測りましょう

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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
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?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address