首页 > 其他分享 >sicp每日一题[2.42]

sicp每日一题[2.42]

时间:2024-10-12 08:53:05浏览次数:1  
标签:2.42 每日 位置 sicp rest 王后 board define queens

这道题太难了,我自己只完成了 empty-board 这一个定义,其他的函数即使看了别人的答案也研究了半天才搞明白。。

; board-size 指的是正方形棋盘的长
(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)         ; rest-of-queens 表示已经被放置在安全位置的 k-1 个王后的位置的组合
            (map (lambda (new-row)
                   (adjoin-position new-row k rest-of-queens))
                 (enumerate-interval 1 board-size)))             ; 把第 k 列的新位置加进之前的王后的位置组合
          (queen-cols (- k 1))))))
  (queen-cols board-size))

(define empty-board nil)

; 检查2个位置坐标是否冲突
; 同行比较好判断,只要新王后位置的行跟前 k-1 个王后的行都不同就行,同列同理
; 对角线比较复杂,分为从左上到右下和从左下到右上两类
; 从左上到右下,在同一条对角线上的坐标,行与列坐标之差相同
; 从左下到右上,在同一条对角线上的坐标,行与列坐标之和相同
(define (check pos1 pos2)
  (let ((x1 (car pos1))
        (y1 (cadr pos1))
        (x2 (car pos2))
        (y2 (cadr pos2)))
    (and (not (= x1 x2))
         (not (= y1 y2))
         (not (= (- x1 x2) (- y1 y2)))
         (not (= (+ x1 x2) (+ y1 y2))))))

; 检查新加入的王后的位置与其他王后的位置是否冲突
; k 其实没有任何作用
(define (safe? k positions)
  (let ((new-queen (car positions))
        (rest-of-queens (cdr positions)))
    (accumulate (lambda (pos result)
                  (and (check pos new-queen)
                       result))
                true
                rest-of-queens)))
         
; 在已有前 k-1 个王后的位置组合的基础上,把某个位置坐标(row col)加到第一个位置
(define (adjoin-position row col rest-of-queens)
  (cons (list row col) rest-of-queens))
 

(queens 1)
(queens 2)
(queens 3)
(queens 4)
(queens 8)


; 执行结果
'(((1 1)))
'()
'()
'(((1 4) (3 3) (4 2) (2 1)) ((1 4) (4 3) (2 2) (3 1)) ((2 4) (3 3) (1 2) (4 1)) ((3 4) (1 3) (2 2) (4 1)))
'(((1 8) (2 7) (6 6) (3 5) (5 4) (7 3) (8 2) (4 1))
  ((1 8) (2 7) (4 6) (6 5) (7 4) (3 3) (8 2) (5 1))
  ((1 8) (2 7) (6 6) (3 5) (7 4) (4 3) (8 2) (5 1))
  ((1 8) (2 7) (7 6) (3 5) (4 4) (6 3) (8 2) (5 1))
  ((1 8) (3 7) (4 6) (5 5) (7 4) (2 3) (8 2) (6 1))
  ((1 8) (4 7) (2 6) (7 5) (5 4) (3 3) (8 2) (6 1))
  ((1 8) (5 7) (2 6) (4 5) (7 4) (3 3) (8 2) (6 1))
  ((1 8) (3 7) (7 6) (2 5) (4 4) (5 3) (8 2) (6 1))
  ((1 8) (2 7) (4 6) (5 5) (8 4) (6 3) (3 2) (7 1))
  ((1 8) (4 7) (2 6) (5 5) (6 4) (8 3) (3 2) (7 1))
  ((1 8) (3 7) (6 6) (2 5) (5 4) (8 3) (4 2) (7 1))
  ((1 8) (2 7) (4 6) (6 5) (8 4) (3 3) (5 2) (7 1))
  ((1 8) (2 7) (6 6) (3 5) (8 4) (4 3) (5 2) (7 1))
  ((1 8) (3 7) (6 6) (4 5) (2 4) (8 3) (5 2) (7 1))
  ((1 8) (6 7) (2 6) (3 5) (4 4) (8 3) (5 2) (7 1))
  ((1 8) (2 7) (4 6) (8 5) (5 4) (3 3) (6 2) (7 1))
  ((2 8) (4 7) (1 6) (5 5) (6 4) (7 3) (3 2) (8 1))
  ((3 8) (1 7) (6 6) (2 5) (5 4) (7 3) (4 2) (8 1))
  ((3 8) (1 7) (6 6) (4 5) (2 4) (7 3) (5 2) (8 1))
  ((2 8) (6 7) (1 6) (3 5) (4 4) (7 3) (5 2) (8 1))
  ((3 8) (1 7) (4 6) (5 5) (7 4) (2 3) (6 2) (8 1))
  ((2 8) (4 7) (1 6) (7 5) (5 4) (3 3) (6 2) (8 1))
  ((2 8) (5 7) (1 6) (4 5) (7 4) (3 3) (6 2) (8 1))
  ((3 8) (1 7) (7 6) (2 5) (4 4) (5 3) (6 2) (8 1))
  ((4 8) (1 7) (3 6) (5 5) (6 4) (2 3) (7 2) (8 1))
  ((2 8) (4 7) (5 6) (1 5) (6 4) (3 3) (7 2) (8 1))
  ((4 8) (1 7) (5 6) (2 5) (6 4) (3 3) (7 2) (8 1))
  ((5 8) (1 7) (2 6) (4 5) (6 4) (3 3) (7 2) (8 1))
  ((2 8) (3 7) (6 6) (4 5) (1 4) (5 3) (7 2) (8 1))
  ((2 8) (4 7) (6 6) (1 5) (3 4) (5 3) (7 2) (8 1))
  ((4 8) (1 7) (6 6) (2 5) (3 4) (5 3) (7 2) (8 1))
  ((2 8) (6 7) (3 6) (1 5) (4 4) (5 3) (7 2) (8 1)))

标签:2.42,每日,位置,sicp,rest,王后,board,define,queens
From: https://www.cnblogs.com/think2times/p/18459724

相关文章

  • 每日算法 88.合并两个有序数组 - Lcode
    给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了......
  • sicp每日一题[2.40]
    Exercise2.40Defineaprocedureunique-pairsthat,givenanintegern,generatesthesequenceofpairs(i,j)with1<j<i<n.Useunique-pairstosimplifythedefinitionofprime-sum-pairsgivenabove.这道题还是挺简单的,最难的部分书上已经实现了,我们只......
  • sicp每日一题[2.38-2.39]
    Exercise2.38Theaccumulateprocedureisalsoknownasfold-right,becauseitcombinesthefirstelementofthesequencewiththeresultofcombiningalltheelementstotheright.Thereisalsoafold-left,whichissimilartofold-right,exceptthati......
  • Python 工具库每日推荐【openpyxl 】
    文章目录引言PythonExcel处理库的重要性今日推荐:openpyxl工具库主要功能:使用场景:安装与配置快速上手示例代码代码解释实际应用案例案例:自动生成月度销售报告案例分析高级特性条件格式数据验证扩展阅读与资源优缺点分析优点:缺......
  • 每日学学Java开发规范,集合处理(附阿里巴巴Java开发手册(终极版))
    前言每次去不同的公司,码不同的代码,适应不同的规范,经常被老大教育规范问题,我都有点走火入魔的感觉,还是要去看看阿里巴巴Java开发规范,从中熟悉一下,纠正自己,码出高效,码出质量。想细看的可以去官网下载,或者下面自取阿里巴巴Java开发手册(终极版)五、集合处理【强制】关于ha......
  • 每日读则推(六)——Consider victims of natural disasters
    "Weneedtobethinkingabout[victimsofnaturaldisasters]longafterthoseinitial                           n.受害者         n.灾难,灾害              ad......
  • sicp每日一题[2.36-2.37]
    果然习惯不能停,就两天没学,昨天就忘的干干净净了。。今天把昨天的补上Exercise2.36Theprocedureaccumulate-nissimilartoaccumulateexceptthatittakesasitsthirdargumentasequenceofsequences,whichareallassumedtohavethesamenumberofelements......
  • 20241006每日一题洛谷P1031
    对题目第一印象:贪心吧,或者纯模拟第一次贪心,由于左右端堆只能想内一堆移动,遂放弃第一次模拟,开多个数组,(可能还要用递归?),遂放弃于是求助如来佛祖:从逻辑上可以看出,第一堆缺或者多的牌只能移动到第二堆上当第一堆达到期望值时,第一堆便不能再做操作,于是,可以将第二堆作为第一堆来处理......
  • 20241002每日一题洛谷P1563
    粗看题目我靠,什么方向还变来变去的(哭泣核心思想:圈内右数,圈外左数为整体逆时针数;圈外右数,圈内左数为整体顺时针数运用结构体就有了第一版源码:structpeople{ intface; charname[12];};intmain(){ intm,n; scanf("%d%d",&n,&m); peoplea[10010]; for(inti......
  • 20241002每日一题洛谷P1563
    粗看题目我靠,什么方向还变来变去的(哭泣核心思想:圈内右数,圈外左数为整体逆时针数;圈外右数,圈内左数为整体顺时针数运用结构体就有了第一版源码://///define_CRT_SECURE_NO_WARNINGS1include<stdio.h>includestructpeople{intface;charname[12];};intmain(){in......