sicp每日一题[2.43]
Exercise 2.43
Louis Reasoner is having a terrible time doing Exercise 2.42. His queens procedure seems to work, but it runs extremely slowly. (Louis never does manage to wait long enough for it to solve even the 6 × 6 case.) When Louis asks Eva Lu Ator for help, she points out that he has interchanged the order of the nested mappings in the flatmap, writing it as
(flatmap
(lambda (new-row)
(map (lambda (rest-of-queens)
(adjoin-position new-row k rest-of-queens))
(queen-cols (- k 1))))
(enumerate-interval 1 board-size))
Explain why this interchange makes the program run slowly. Estimate how long it will take Louis’s program to solve theeight-queens puzzle, assuming that the program in Exercise 2.42 solves the puzzle in time T.
慢的原因比较好理解,Louis 的答案每一次都要把 queen-cols 计算
(enumerate-interval 1 board-size)
遍,一共算了 board-size^board-size 遍,对于八皇后问题就是 8^8 遍;对于练习 2.42 的原方法,每次循环都会减一次,所以一共计算了 n! 次,如果原方法时间为 T,那么 Louis 的方法要用的时间是 8^8/8!*T.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?