11/*
22 SICP JS Exercise 4.35
3-
43 This file contains two solutions for the n-queens puzzle using non-determinism.
54 Each of them makes use of the generate-and-test paradigm.
6-
75 The first (queens_slow) uses non-determinism for generating both the row and column
86 for each position. It does so in an optimized manner, testing the corresponding condition
97 as soon as the choice is generated, thereby reducing the search space. However, this is not
108 enough to yield a quick solution when N = 8.
119 This solution also gives repeated solutions (i.e different permutations that have all the same positions) upon backtracking.
12-
1310 The second (queens_fast) uses non-determinism in picking only the row and not the column of each position, with the
1411 columns being fixed. This further reduces the search space, yielding a quick solution when N = 8.
1512*/
1613
17- /* Pretty prints a solution to the n-queens puzzle */
18- function pretty_print ( result , board_size ) {
19- function member_eq ( v , xs ) {
20- return is_null ( xs )
21- ? null
22- : equal ( v , head ( xs ) )
23- ? xs
24- : member_eq ( v , tail ( xs ) ) ;
25- }
26- const possible_positions = enum_list ( 1 , board_size ) ;
27-
28- let col_index_str = " " ;
29- for_each ( i => {
30- col_index_str = col_index_str + stringify ( i ) + " " ;
31- } , possible_positions ) ;
32- display ( col_index_str ) ;
33-
34- for_each ( row => {
35- let row_str = stringify ( row ) + " " ;
36- for_each ( col => {
37- const position = pair ( row , col ) ;
38- const contains_position = member_eq ( position , result ) !== null ;
39- if ( contains_position ) {
40- row_str = row_str + "Q " ;
41- } else {
42- row_str = row_str + ". " ;
43- }
44- } , possible_positions ) ;
45-
46- display ( row_str ) ;
47- } , possible_positions ) ;
48- }
49-
5014const N = 8 ; // the number of queens and the size of the board
5115const empty_positions = null ;
5216
@@ -161,3 +125,37 @@ function is_safe(new_position, positions) {
161125 positions
162126 ) ;
163127}
128+
129+
130+ /* Pretty prints a solution to the n-queens puzzle */
131+ function pretty_print ( result , board_size ) {
132+ function member_eq ( v , xs ) {
133+ return is_null ( xs )
134+ ? null
135+ : equal ( v , head ( xs ) )
136+ ? xs
137+ : member_eq ( v , tail ( xs ) ) ;
138+ }
139+ const possible_positions = enum_list ( 1 , board_size ) ;
140+
141+ let col_index_str = " " ;
142+ for_each ( i => {
143+ col_index_str = col_index_str + stringify ( i ) + " " ;
144+ } , possible_positions ) ;
145+ display ( col_index_str ) ;
146+
147+ for_each ( row => {
148+ let row_str = stringify ( row ) + " " ;
149+ for_each ( col => {
150+ const position = pair ( row , col ) ;
151+ const contains_position = member_eq ( position , result ) !== null ;
152+ if ( contains_position ) {
153+ row_str = row_str + "Q " ;
154+ } else {
155+ row_str = row_str + ". " ;
156+ }
157+ } , possible_positions ) ;
158+
159+ display ( row_str ) ;
160+ } , possible_positions ) ;
161+ }
0 commit comments