-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnqueen.py
575 lines (483 loc) · 17.8 KB
/
nqueen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#!/usr/bin/python
"""The N-Queens problem. Version 2.5"""
import argparse
import sys
import time
# import random
# Constants
# The maximum number of columns on the chessboard.
_MAX_COLUMNS = 20
# The maximum number of rows on the chessboard.
_MAX_ROWS = 20
# FYI: There are 39,029,188,884 solutions for 20x20 !
# Globals
# NB: Everything is initialized in initialize(), so these
# are sometimes just set to empty values and defaults.
# Do we show progress?
show_progress = False
# Do we print the board solutions?
no_print = False
# Do we display node counts?
show_node_count = False
# Do we display total node counts?
show_total_node_count = False
# Actual number of columns on the chessboard.
number_of_columns = 8
# Actual number of rows on the chessboard.
number_of_rows = 8
# The row location of the Queen in a particular column.
# -1 if there is no Queen in that column.
# This 'array' is indexed starting at 0!
# It is actually a list in Python.
# Will be queen_location = [-1] * number_of_columns
queen_location = []
# The saved search list of admissible rows for a column.
# Will be admissible_rows = [[] for _ in range(number_of_columns)]
# NB: admissible_rows = [] * (number_of_columns) does NOT work; it gives [].
admissible_rows = []
# The next row location to try putting the Queen.
try_row = 0
# The next column to try placing a Queen.
queen_column = 0
# Count the number of nodes visited for one solution.
# We count as a "node" every time we place a Queen on the board.
node_count = 0
# Count the total number of nodes visited.
total_node_count = 0
# A count used to make neat output for the progress functions.
progress_count = 0
# Count the number of solutions when asking for all of them.
solution_count = 0
# Find all solutions?
find_all_solutions = False
# Optimization helper matrices
# These look upside down because in chess the rows are numbered from
# the White side up, while in Python (and Math matrices) the numbering
# of rows is top down!
# slash_code assigns to each square on the board a diagonal number.
# These are positive slope diagonals. "/" oriented; hence the name.
# The values are indices into the "*_lookup" arrays below.
# Will be...
# slash_code = [[0 for _ in range(0, number_of_columns)]
# for _ in range(0, number_of_rows)]
# backslash_code assigns to each square on the board a diagonal number.
# These are negative slope diagonals. (anti-diagonals in chess programming)
# "\" oriented; hence the name.
# backslash_code = [[0 for _ in range(0, number_of_columns)]
# for _ in range(0, number_of_rows)]
slash_code = []
backslash_code = []
# row_lookup records if a row is occupied by some Queen.
# Will be...
# row_lookup = [False] * (number_of_rows)
# slash_code_lookup records if a positive diagonal is occupied by some Queen.
# slash_code_lookup = [False] * (number_of_columns + number_of_rows - 1)
# backslash_code_lookup records if a negative diagonal is occupied.
# backslash_code_lookup = [False] * (number_of_columns + number_of_rows - 1)
row_lookup = []
slash_code_lookup = []
backslash_code_lookup = []
# row_indices caches the entire list of row numbers for efficiency.
# Will be row_indices = list(range(0,number_of_rows))
row_indices = []
def show_advancing():
"""Show that we are advancing in finding a solution"""
global progress_count
if show_progress:
print("+{}".format(queen_column), end='')
# This next is to get neat output.
progress_count += 1
if progress_count > 25:
print()
progress_count = 0
def show_retreating():
"""Show that we are backtracking"""
global progress_count
if show_progress:
print("-{}".format(queen_column), end='')
# This next is to get neat output.
progress_count += 1
if progress_count > 25:
print()
progress_count = 0
def show_found_solution():
"""Show that we have found a solution"""
if show_progress:
print("!")
if show_node_count:
print("Searched {}".format(node_count), "nodes.")
def show_no_solution():
"""Show that we did not find a solution"""
if show_progress:
print("@")
if show_node_count:
print("Searched {}".format(node_count), "nodes.")
def show_no_more_solutions():
"""Show that we did not find more solutions"""
if show_progress:
print("#")
if show_node_count:
print("Searched {}".format(node_count), "nodes.")
def print_solution():
"""Print a solution"""
if no_print:
return
for row in range(0, number_of_rows):
for column in range(0, number_of_columns):
if queen_location[column] == row:
print("Q", end='')
else:
print("-", end='')
print()
print("---------------------")
def retreat(success_flag):
"""retreat() backs up the search tree looking for a "previous column"
with rows that have not been tried. This previous column might be
the last column on the board if a solution was just found!
If retreat() ever finds that it cannot back up any further then it
notifies of either no solution, or no more solutions if one had
already been found so indicated by success_flag, and returns False.
Otherwise as long as retreat() is searching, it first removes the Queen
and associated attacking information for further tries.
If a new column and row to try are found then queen_column contains
the column number, try_row the row to try and True is returned. In
that case, admissible_rows[] for that column is also
updated.
show_retreating() is called for every queen_column decrement.
Note: success_flag is passed in True if we've found some solution
already; otherwise it is False.
retreat() is complicated by handling different behavior needed
because of adding the ability to find all solutions, and for
performance.
"""
global queen_column
global try_row
global admissible_rows
global row_lookup
global slash_code_lookup
global backslash_code_lookup
global queen_location
# We have to go back to the "previous column"; which may be the final
# column on the board.
queen_column -= 1
show_retreating()
while queen_column > -1:
# Can retreat.
# Remove the Queen from the board.
queen_row = queen_location[queen_column]
row_lookup[queen_row] = False
slash_code_lookup[slash_code[queen_row][queen_column]] = False
backslash_code_lookup[backslash_code[queen_row][queen_column]] = False
queen_location[queen_column] = -1
# Try the next admissible row after the one we tried last.
search_list = admissible_rows[queen_column]
if search_list:
# More to try.
# Get the next try_row. NB: This also changes
# admissible_rows[queen_column] since search_list is a
# reference!
try_row = search_list.pop(0)
return True
else:
queen_column -= 1
show_retreating()
# Cannot retreat.
if find_all_solutions:
if success_flag:
# We already found a solution.
show_no_more_solutions()
else:
# No solution.
show_no_solution()
else:
# No solution.
show_no_solution()
return False
def get_search_list_2():
"""Get the admissible rows by filtering several functions
iteratively and short circuiting early if none free.
"""
def row_check(sr):
return not row_lookup[sr]
def slash_check(sr):
return not slash_code_lookup[slash_code[sr][queen_column]]
def backslash_check(sr):
return not backslash_code_lookup[backslash_code[sr][queen_column]]
empty_list = []
search_list = list(filter(row_check, row_indices))
if search_list:
# Got free ones so far.
search_list = list(filter(slash_check, search_list))
if search_list:
# Got free ones so far.
search_list = list(filter(backslash_check, search_list))
if search_list:
# Got non empty search_list.
return search_list
else:
# Nothing free.
return empty_list
else:
# Nothing free.
return empty_list
else:
# Nothing free.
return empty_list
def get_search_list_1():
"""Get the admissible rows by filtering a function over the
row_indices checking for freedom in all directions at once.
"""
def free_row(sr):
return not (slash_code_lookup[slash_code[sr][queen_column]]
or backslash_code_lookup[backslash_code[sr][queen_column]]
or row_lookup[sr])
# Get all the remaining admissible rows for queen_column; if any.
search_list = list(filter(free_row, row_indices))
return search_list
def initialize_admissible_rows():
"""For the current number_of_rows and queen_column, finds all the
admissible rows. Then if there are any such rows, pop the first
admissible row into try_row and save the remainder in
admissible_rows[queen_column]; return True. Otherwise, return
False.
"""
global try_row
global admissible_rows
# Get all the remaining admissible rows for queen_column; if any.
search_list = get_search_list_1()
# random.shuffle(search_list)
if search_list:
# Set the next row location to try putting the Queen.
# Last or first; doesn't seem to matter in finding all solutions.
# try_row = search_list.pop()
try_row = search_list.pop(0)
# Save the remaining admissible rows if any.
admissible_rows[queen_column] = search_list
return True
else:
# No admissible rows.
return False
def store_and_check_for_solution():
"""Place the current Queen on the board. If this is a solution
return True; otherwise return False
"""
global queen_location
global row_lookup
global slash_code_lookup
global backslash_code_lookup
global queen_column
global node_count
global total_node_count
show_advancing()
queen_location[queen_column] = try_row
row_lookup[try_row] = True
slash_code_lookup[slash_code[try_row][queen_column]] = True
backslash_code_lookup[backslash_code[try_row][queen_column]] = True
queen_column += 1
node_count += 1
total_node_count += 1
if queen_column <= number_of_columns - 1:
# No solution yet.
return False
else:
# We have a solution.
return True
def search():
"""Search for a solution and return True if found else False"""
global solution_count
global node_count
# Return value.
# True if we find a solution; False otherwise.
success_flag = False
more_to_search = True
while more_to_search:
assert try_row <= number_of_rows - 1
# Store the info and see if we have a solution.
if not store_and_check_for_solution():
# No solution yet.
if initialize_admissible_rows():
pass
else:
# We could not place the next Queen anywhere.
# Try to back out the last Queen assignment and prepare
# to try the next possibility.
more_to_search = retreat(success_flag)
else:
# We found a solution.
success_flag = True
show_found_solution()
print_solution()
solution_count += 1
node_count = 0
if find_all_solutions:
more_to_search = retreat(success_flag)
else:
more_to_search = False
return success_flag
def initialize(size):
"""Initialize for the next problem of a given size"""
global number_of_columns
global number_of_rows
global queen_location
global admissible_rows
global queen_column
global node_count
global total_node_count
global progress_count
global solution_count
global slash_code
global backslash_code
global slash_code_lookup
global backslash_code_lookup
global row_indices
global row_lookup
global try_row
# Set the size of the chessboard.
number_of_columns = size
number_of_rows = size
# Empty the Queen locations.
queen_location = [-1] * number_of_columns
# Empty the admissible rows.
admissible_rows = [[] for _ in range(number_of_columns)]
# The next column to try placing a Queen.
queen_column = 0
# Set our counters and such.
node_count = 0
total_node_count = 0
progress_count = 0
solution_count = 0
# Set our helper matrices for optimization.
slash_code = [[0 for _ in range(0, number_of_columns)]
for _ in range(0, number_of_rows)]
backslash_code = [[0 for _ in range(0, number_of_columns)]
for _ in range(0, number_of_rows)]
for r in range(0, number_of_rows):
for c in range(0, number_of_columns):
slash_code[r][c] = r + c
backslash_code[r][c] = r - c + number_of_columns - 1
slash_code_lookup = [False] * (number_of_columns + number_of_rows - 1)
backslash_code_lookup = [False] * (number_of_columns + number_of_rows - 1)
row_lookup = [False] * number_of_rows
row_indices = list(range(0, number_of_rows))
# Initialize admissible_rows and try_row.
if not initialize_admissible_rows():
print("Error: initialization fail!!")
def check_num(s):
"""Check that a string is an integer."""
try:
int(s)
return True
except ValueError:
return False
def sanity_check_args(start, number_of_problems):
"""Sanity check the user arguments."""
if check_num(start):
start = int(start)
else:
print('error: {}'.format(start), ' is not a number.')
sys.exit(1)
if check_num(number_of_problems):
number_of_problems = int(number_of_problems)
else:
print('error: {}'.format(number_of_problems), ' is not a number.')
sys.exit(1)
if start <= 0:
print('error: starting value must be greater than 0')
sys.exit(1)
if number_of_problems < 1:
print('error: number of problem sizes must be greater than 0')
sys.exit(1)
if ((start + number_of_problems) - 1) > _MAX_COLUMNS:
print('error: Sorry. Limit is {} columns'.format(_MAX_COLUMNS))
sys.exit(1)
# noinspection SpellCheckingInspection
# Previous comment is for IntelliJ to stop complaining.
def run(start=8, number_of_problems=1,
fas=False,
sp=False,
np=False,
snc=False,
stnc=False):
global find_all_solutions
global show_progress
global no_print
global show_node_count
global show_total_node_count
find_all_solutions = fas
show_progress = sp
no_print = np
show_node_count = snc
show_total_node_count = stnc
start_time = time.time()
for size in range(start, start + number_of_problems):
initialize(size)
if find_all_solutions:
print("Looking for all solutions "
+ "to the {}-Queen problem.".format(size))
else:
print("Looking for a solution "
+ "to the {}-Queen problem.".format(size))
if search():
# Got at least one solution.
if find_all_solutions:
print("Found all solutions to the {}-Queen".format(size),
"problem.")
else:
pass
else:
# No solution.
print("No solution to the {}-Queen".format(size), "problem.")
if find_all_solutions:
print("Total solutions found to the",
"{}-Queen problem is".format(size),
"{}".format(solution_count))
if show_total_node_count:
print("Searched {}".format(total_node_count), "total nodes.")
print("----------------------------------------")
end_time = time.time()
print("Execution time in secs was {}".format(end_time-start_time))
def run_with_args(args):
"""Run the program with args already parsed"""
global find_all_solutions
global show_progress
global no_print
global show_node_count
global show_total_node_count
sanity_check_args(args.start, args.number_of_problems)
start = int(args.start)
number_of_problems = int(args.number_of_problems)
if args.all:
find_all_solutions = True
if args.show_progress:
show_progress = True
if args.no_print:
no_print = True
if args.show_node_count:
show_node_count = True
if args.show_total_node_count:
show_total_node_count = True
run(start, number_of_problems, find_all_solutions,
show_progress, no_print, show_node_count,
show_total_node_count)
def main():
"""Main code with command line parser"""
parser = argparse.ArgumentParser(description="Solve N-queen problems")
parser.add_argument('start', type=int,
help='minimum board starting size')
parser.add_argument('number_of_problems', type=int,
help='number of problems sizes to do')
parser.add_argument('--all', action='store_true',
help="find all solutions")
parser.add_argument('--show_progress', action='store_true',
help="show progress")
parser.add_argument('--show_node_count', action='store_true',
help="show node counts")
parser.add_argument('--show_total_node_count', action='store_true',
help="show total node counts")
parser.add_argument('--no_print', action='store_true',
help="do not print boards")
parser.set_defaults(func=run_with_args)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()