-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNQueenUserInput.py
78 lines (65 loc) · 1.54 KB
/
NQueenUserInput.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
n = int(input("Enter the value of n: "))
board = []
def getBoard():
for i in range(n):
nList = []
for j in range(n):
nList.append(0)
board.append(nList)
def printBoard():
for i in range(n):
for j in range(n):
print(board[i][j], end= " ")
print(" ")
def isSafe(row, column):
for i in range(n):
if board[row][i] == 1:
return False
for j in range(n):
if board[j][column] == 1:
return False
i = row-1
j = column-1
while i >= 0 and j >= 0:
if board[i][j] == 1:
return False
i = i-1
j = j-1
i = row-1
j = column+1
while i>=0 and j<n:
if board[i][j] == 1:
return False
i = i-1
j = j+1
i = row+1
j = column-1
while i<n and j>=0:
if board [i][j] == 1:
return False
i = i+1
j = j-1
i = row+1
j = column+1
while i<n and j<n:
if board[i][j] == 1:
return False
i = i+1
j = j+1
return True
def Put(n, count):
if count == n:
return True
for i in range(n):
for j in range(n):
if isSafe(i,j):
board[i][j] = 1
count = count+1
if Put(n, count) == True:
return True
board[i][j] = 0
count = count-1
return False
getBoard()
Put(n, 0)
printBoard()