- 浏览: 88777 次
- 性别:
- 来自: 北京
-
最新评论
-
Bloodwolf:
gaowei52306 写道怎样在Linux中设置路由,用此方 ...
Loadrunner IP欺骗 -
gaowei52306:
怎样在Linux中设置路由,用此方法在Windows操作系统上 ...
Loadrunner IP欺骗
![]() |
The ``eight-queens puzzle'' asks how to place eight queens on a chessboard so that no queen is in check from any other (i.e., no two queens are in the same row, column, or diagonal). One possible solution is shown in figure 2.8. One way to solve the puzzle is to work across the board, placing a queen in each column. Once we have placed k - 1 queens, we must place the kth queen in a position where it does not check any of the queens already on the board. We can formulate this approach recursively: Assume that we have already generated the sequence of all possible ways to place k - 1 queens in the first k - 1 columns of the board. For each of these ways, generate an extended set of positions by placing a queen in each row of the kth column. Now filter these, keeping only the positions for which the queen in the kth column is safe with respect to the other queens. This produces the sequence of all ways to place k queens in the first k columns. By continuing this process, we will produce not only one solution, but all solutions to the puzzle.
We implement this solution as a procedure queens, which returns a sequence of all solutions to the problem of placing n queens on ann× n chessboard. Queens has an internal procedure queen-cols that returns the sequence of all ways to place queens in the first kcolumns of the board.
(define (queens board-size)
(define (queen-cols k)
(if (= k 0)
(list empty-board)
(filter
(lambda (positions) (safe? k positions))
(flatmap
(lambda (rest-of-queens)
(map (lambda (new-row)
(adjoin-position new-row k rest-of-queens))
(enumerate-interval 1 board-size)))
(queen-cols (- k 1))))))
(queen-cols board-size))
In this procedure rest-of-queens is a way to place k - 1 queens in the first k - 1 columns, and new-row is a proposed row in which to place the queen for the kth column. Complete the program by implementing the representation for sets of board positions, including the procedure adjoin-position, which adjoins a new row-column position to a set of positions, and empty-board, which represents an empty set of positions. You must also write the procedure safe?, which determines for a set of positions, whether the queen in thekth column is safe with respect to the others. (Note that we need only check whether the new queen is safe -- the other queens are already guaranteed safe with respect to each other.)
八皇后问题,结果只保存了行,列即是list的下标
(define (queens board-size) (define (queen-cols k) (if (= k 0) (list empty-board) (filter (lambda (positions) (safe? k positions)) (flatmap (lambda (rest-of-queens) (map (lambda (new-row) (adjoin-position new-row k rest-of-queens)) (enumerate-interval 1 board-size))) (queen-cols (- k 1)))))) (queen-cols board-size)) (define empty-board '()) (define (adjoin-position new-row k rest-of-queens) (append rest-of-queens (list new-row))) (define (safe? k positions) (if (< (length positions) 2) #t (let ((new (list-ref positions (- k 1))) (first (car positions))) (cond ((= new first) #f) ((= new (+ first (- (length positions) 1))) #f) ((= new (- first (- (length positions) 1))) #f) (else (safe? (- k 1) (cdr positions))))))) (define (filter predicate sequence) (cond ((null? sequence) '()) ((predicate (car sequence)) (cons (car sequence) (filter predicate (cdr sequence)))) (else (filter predicate (cdr sequence))))) (define (flatmap proc seq) (accumulate append '() (map proc seq))) (define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) (define (enumerate-interval n m) (if (> n m) '() (cons n (enumerate-interval (+ n 1) m)))) (queens 8)
((1 5 8 6 3 7 2 4)
(1 6 8 3 7 4 2 5)
(1 7 4 6 8 2 5 3)
(1 7 5 8 2 4 6 3)
(2 4 6 8 3 1 7 5)
(2 5 7 1 3 8 6 4)
(2 5 7 4 1 8 6 3)
(2 6 1 7 4 8 3 5)
(2 6 8 3 1 4 7 5)
(2 7 3 6 8 5 1 4)
(2 7 5 8 1 4 6 3)
(2 8 6 1 3 5 7 4)
(3 1 7 5 8 2 4 6)
(3 5 2 8 1 7 4 6)
(3 5 2 8 6 4 7 1)
(3 5 7 1 4 2 8 6)
(3 5 8 4 1 7 2 6)
(3 6 2 5 8 1 7 4)
(3 6 2 7 1 4 8 5)
(3 6 2 7 5 1 8 4)
(3 6 4 1 8 5 7 2)
(3 6 4 2 8 5 7 1)
(3 6 8 1 4 7 5 2)
(3 6 8 1 5 7 2 4)
(3 6 8 2 4 1 7 5)
(3 7 2 8 5 1 4 6)
(3 7 2 8 6 4 1 5)
(3 8 4 7 1 6 2 5)
(4 1 5 8 2 7 3 6)
(4 1 5 8 6 3 7 2)
(4 2 5 8 6 1 3 7)
(4 2 7 3 6 8 1 5)
(4 2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 2 5 3 1 7 4 6)
(8 3 1 6 2 5 7 4)
(8 4 1 3 6 2 7 5))
发表评论
-
sicp 2.53
2013-09-13 11:22 710Exercise 2.53. What would the ... -
sicp 2.52
2013-09-13 10:59 625Exercise 2.52. Make changes t ... -
sicp 2.51
2013-09-12 17:39 581Exercise 2.51. Define the be ... -
sicp 2.50
2011-12-06 15:33 763Exercise 2.50. Define the tran ... -
sicp 2.49
2011-12-06 15:25 809Exercise 2.49. Use segments ... -
sicp 2.48
2011-11-28 18:07 747Exercise 2.48. A directed line ... -
sicp 2.47
2011-11-28 18:02 786Exercise 2.47. Here are two ... -
sicp 2.46
2011-11-28 17:53 816Exercise 2.46. A two-dimens ... -
sicp 2.45
2011-11-28 17:47 747Exercise 2.45. Right-split ... -
sicp 2.44
2011-11-28 17:08 746Exercise 2.44. Define the proc ... -
sicp 2.43
2011-11-25 14:08 1108Exercise 2.43. Louis Reason ... -
sicp 2.41
2011-11-21 16:58 935Exercise 2.41. Write a procedu ... -
sicp 2.40
2011-11-21 11:46 812Exercise 2.40. Define a proced ... -
sicp 2.39
2011-11-18 01:42 911Exercise 2.39. Complete th ... -
sicp 2.38
2011-11-17 19:01 830Exercise 2.38. The accumula ... -
sicp 2.37
2011-11-17 18:59 645Exercise 2.37. Suppose we r ... -
sicp 2.26
2011-11-17 17:39 712Exercise 2.36. The procedur ... -
sicp 2.35
2011-11-17 17:17 791Exercise 2.35. Redefine cou ... -
sicp 2.34
2011-11-17 16:46 668Exercise 2.34. Evaluating a ... -
sicp 2.33
2011-11-16 15:51 650Exercise 2.33. Fill in the ...
相关推荐
SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版SICP中文第二版
sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 !!!download>>>https://github.com/wizardforcel/sicp-py-zh
《计算机程序的构造与解释》(Structure and Interpretation of Computer Programs,简称SICP)是一本备受推崇的经典计算机科学教材,由Harold Abelson和Gerald Jay Sussman撰写,并由MIT出版社出版。这本书以其深入...
《SICP 2.2.4 节:图形语言》是计算机科学经典教材《结构与解释程序》(Structure and Interpretation of Computer Programs)中的一个重要章节,它深入介绍了如何利用编程来创建图形,以及如何设计和理解复杂的计算...
《SICP解题集》是一份专注于探讨和解答《结构与解释程序》(Structure and Interpretation of Computer Programs,简称SICP)一书中习题的资源。SICP是计算机科学领域的一本经典教材,由Harold Abelson、Gerald Jay ...
《计算机程序的构造和解释》(SICP)是一本极具影响力的计算机科学教材,由Harold Abelson和Gerald Jay Sussman所著,MIT出版社出版。这本书以其深入探讨编程概念、程序设计方法以及计算机系统的工作原理而闻名。1-3...
《计算机程序构造和解释》(SICP,Structure and Interpretation of Computer Programs)是一本具有深远影响力的计算机科学教材,由Harold Abelson和Gerald Jay Sussman编写,MIT Press出版。这门课程由北京大学数学...
《SICP习题解答,主要第一章的内容习题答案》 SICP,全称《Structure and Interpretation of Computer Programs》(计算机程序的构造和解释),是计算机科学领域的一本经典教材,由MIT(麻省理工学院)的 Harold ...
SICP-Python版本
SICP 使用的scheme解释器 以前叫DrScheme
Python SICP epub版本,很适合学习抽象的思想,用Python版本比lisp更实用
《SICP》全称是《Structure and Interpretation of Computer Programs》,中文译为《计算机程序的构造和解释》。这是一本经典的计算机科学教材,由Harvard大学的 Harold Abelson 和 Gerald Jay Sussman 教授撰写,...
标题中的"PyPI 官网下载 | sicp-0.0.1b102.dev4.tar.gz"指的是从Python的官方包索引(Python Package Index,简称PyPI)上下载的一个名为"sicp"的软件包的版本号为0.0.1b102.dev4的压缩文件,其格式是tar.gz。...
本书名为《a_book_sicp_py》,是一本以Python语言为基础介绍设计模式和计算机科学基础的书籍。根据描述和部分内容,可以提炼出以下知识点: 1. 编程语言的重要性:在计算机科学的宽泛领域中,编程语言扮演着至关...
sicp in python 中文版 sicp in python 中文版 sicp in python 中文版 download : https://github.com/wizardforcel/sicp-py-zh
《Structure and Interpretation of Computer Programs》(简称SICP)是计算机科学领域的一部经典教材,由Harold Abelson和Gerald Jay Sussman撰写,第二版(2nd Edition)通常被称为SICP 2nd。这本书是麻省理工学院...
sicp-in-python(中文版+英文版)PDF 背景. SICP 全称Structure and Interpretation of Computer Programs,翻译过来叫《计算机程序的构造和解释》使用python
《SICP(Structure and Interpretation of Computer Programs)》是一本经典的计算机科学教材,由Harold Abelson和Gerald Jay Sussman所著,它强调了程序设计的基础和原理,特别是函数式编程思想。第二章主要探讨了...
《SICP:SICP解决方案》是针对结构与解释程序设计(Structure and Interpretation of Computer Programs,简称SICP)这本书的详细解答和实践指南。SICP是一本经典的计算机科学教材,由Harold Abelson和Gerald Jay ...