-
Notifications
You must be signed in to change notification settings - Fork 2
/
NQueens.py
52 lines (47 loc) · 1.21 KB
/
NQueens.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
def isSafe (board, row, col):
# check left row
for y in range(col):
if board[row][y] == 1:
return False
# check diagonal left top
for x, y in zip(range(row, -1, -1), range(col, -1, -1)):
if board[x][y] == 1:
return False
# check diagonal left bottom
for x, y in zip(range(row, N, 1), range(col, -1, -1)):
if board[x][y] == 1:
return False
return True
def generateSolution(board, col):
# terminating condition
# all columns covered
global N
if col >= N:
return True
# loop over all the rows
for i in range(N):
if isSafe(board, i, col):
board[i][col] = 1
# recursively place other queens
print(board)
if generateSolution(board, col + 1):
return True
# unmark queen spot
board[i][col] = 0
# backtrack
return False
N = int(input())
startCol = 0
board = [[0 for i in range(N)] for j in range(N)]
if not generateSolution(board, startCol):
print("No Solution Exists")
else:
print("Solution exists")
print(board)
'''
output:
[[0, 0, 1, 0],
[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 1, 0, 0]]
'''