-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathN-queens.py
More file actions
77 lines (55 loc) · 1.45 KB
/
N-queens.py
File metadata and controls
77 lines (55 loc) · 1.45 KB
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
#Problem statement 1
import copy
backtrack=0
def get_board(size):
board = [0] * size
for ix in range(size):
board[ix] = [0] * size
return board
def print_solutions(solutions, size):
for sol in solutions:
for row in sol:
print(row)
print()
def is_safe(board, row, col, size):
for iy in range(col):
if board[row][iy] == 1:
return False
ix, iy = row, col
while ix >= 0 and iy >= 0:
if board[ix][iy] == 1:
return False
ix -= 1
iy -= 1
jx, jy = row, col
while jx < size and jy >= 0:
if board[jx][jy] == 1:
return False
jx += 1
jy -= 1
return True
def solve(board, col, size):
global backtrack
if col >= size:
return
for i in range(size):
if is_safe(board, i, col, size):
board[i][col] = 1
if col == size - 1:
add_solution(board)
board[i][col] = 0
return
solve(board, col + 1, size)
backtrack+=1
board[i][col] = 0
def add_solution(board):
global solutions
saved_board = copy.deepcopy(board)
solutions.append(saved_board)
size = int(input('What is the size of the chessboard? n = \n'))
board = get_board(size)
solutions = []
solve(board, 0, size)
print_solutions(solutions, size)
print("Total solutions :",len(solutions))
print('Backtracks are:',backtrack)