灯下 登录
计算机科学 / SICP / 4.3.2 Examples of Nondeterministic Programs

Exercise 4.40 · 习题

Exercise 4.40: In the multiple dwelling problem,

how many sets of assignments are there of people to floors, both before and

after the requirement that floor assignments be distinct? It is very

inefficient to generate all possible assignments of people to floors and then

leave it to backtracking to eliminate them. For example, most of the

restrictions depend on only one or two of the person-floor variables, and can

thus be imposed before floors have been selected for all the people. Write and

demonstrate a much more efficient nondeterministic procedure that solves this

problem based upon generating only those possibilities that are not already

ruled out by previous restrictions. (Hint: This will require a nest of

let expressions.)

练习 4.40:在多重居住问题中,在要求各人楼层互不相同之前,有多少种人与楼层的分配方案?在加入此要求之后呢?先生成所有可能的分配方案再靠回溯来逐一排除是非常低效的。例如,大多数限制条件只依赖一个或两个人-楼层变量,因此可以在还没有为所有人选定楼层之前就施加这些限制。请编写并演示一个效率更高的非确定性过程来解决这一问题,其策略是只生成那些尚未被之前的限制条件排除的可能性。(提示:这需要嵌套的 let 表达式。)