-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathN-QueenUsingQueue.py
More file actions
45 lines (37 loc) · 1.31 KB
/
N-QueenUsingQueue.py
File metadata and controls
45 lines (37 loc) · 1.31 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
# Algorithm
# We rotate the array of initial board - 1,2,4,8,16,......,2^(n-1) => See bit form to visualise the board where 1 shows presence of queen
# 1 => 2,4,8,16,.......,2^(n-1),1 -> initial helper
# 2 => insert 2 in board then rotate again => 8,16,......1,4
# 3 => insert 8 in board then rotate again => 32,64,....1,4,16
# Do this for i = 0 to i < (n+1)/2 -> by inspection
# In above code we iterate till (n-1)/2 as 1st case is done separately
#First selects number at 2,4,6,... and then at 1,3,5,7,.....
from collections import deque
while True:
while True:
n = int(input("Enter n for N-Queen (n > 3) : "))
if n > 3:
break
print("n for N-Queen should be greater than 3")
helper = deque([])
board = deque([])
for i in range(n - 1):
helper.append(2 ** (i + 1))
helper.append(1)
for i in range(int((n - 1) / 2)):
board.append(helper.popleft())
curr = helper.popleft()
helper.append(curr)
while len(helper) > 0:
curr = helper.popleft()
board.append(curr)
while len(board) > 0:
curr = bin(board.popleft())
curr = curr[2:] # Removing 0b from front of bit form
while len(curr) < n:
curr = '0' + curr # Making every bit form of same length
curr = ' '.join(curr) # Adding spaces between every bit
print(curr)
ans = input("Do you want to continue (y/n) : ")
if ans != "y" and ans != "Y":
break