forked from adarshpandey10t/Hacktoberfestmine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNQueen.py
More file actions
119 lines (87 loc) · 3.15 KB
/
NQueen.py
File metadata and controls
119 lines (87 loc) · 3.15 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
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
# N-Queen problem in Python
import math
result = []
# Program to solve N-Queens Problem
def solveBoard(board, row, rowmask,
ldmask, rdmask):
n = len(board)
# All_rows_filled is a bit mask
# having all N bits set
all_rows_filled = (1 << n) - 1
# If rowmask will have all bits set, means
# queen has been placed successfully
# in all rows and board is displayed
if (rowmask == all_rows_filled):
v = []
for i in board:
for j in range(len(i)):
if i[j] == 'Q':
v.append(j+1)
result.append(v)
# We extract a bit mask(safe) by rowmask,
# ldmask and rdmask. all set bits of 'safe'
# indicates the safe column index for queen
# placement of this iteration for row index(row).
safe = all_rows_filled & (~(rowmask |
ldmask | rdmask))
while (safe > 0):
# Extracts the right-most set bit
# (safe column index) where queen
# can be placed for this row
p = safe & (-safe)
col = (int)(math.log(p)/math.log(2))
board[row][col] = 'Q'
# This is very important:
# we need to update rowmask, ldmask and rdmask
# for next row as safe index for queen placement
# will be decided by these three bit masks.
# We have all three rowmask, ldmask and
# rdmask as 0 in beginning. Suppose, we are placing
# queen at 1st column index at 0th row. rowmask, ldmask
# and rdmask will change for next row as below:
# rowmask's 1st bit will be set by OR operation
# rowmask = 00000000000000000000000000000010
# ldmask will change by setting 1st
# bit by OR operation and left shifting
# by 1 as it has to block the next column
# of next row because that will fall on left diagonal.
# ldmask = 00000000000000000000000000000100
# rdmask will change by setting 1st bit
# by OR operation and right shifting by 1
# as it has to block the previous column
# of next row because that will fall on right diagonal.
# rdmask = 00000000000000000000000000000001
# these bit masks will keep updated in each
# iteration for next row
solveBoard(board, row+1, rowmask | p,
(ldmask | p) << 1, (rdmask | p) >> 1)
# Reset right-most set bit to 0 so, next
# iteration will continue by placing the queen
# at another safe column index of this row
safe = safe & (safe-1)
# Backtracking, replace 'Q' by ' '
board[row][col] = ' '
# Program to print board
def printBoard(board):
for row in board:
print("|" + "|".join(row) + "|")
# Driver Code
def main():
n = 4 # board size
board = []
for i in range(n):
row = []
for j in range(n):
row.append(' ')
board.append(row)
rowmask = 0
ldmask = 0
rdmask = 0
row = 0
# Function Call
result.clear()
solveBoard(board, row, rowmask, ldmask, rdmask)
result.sort()
print(result)
if __name__ == "__main__":
main()