-
Notifications
You must be signed in to change notification settings - Fork 2
/
Assignment-4.py
57 lines (52 loc) · 1.18 KB
/
Assignment-4.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
# Number of queens
n=4
# Matrix
a=[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
# Dictionary for backtrack
b={}
# Checking if column is safe
def isColumnSafe(r,c):
while(r>=0):
if(a[r][c] == 1):
return 0
r = r-1
return 1
# Checking if left diagonal is safe
def isLeftDiagonalSafe(r,c):
while(r>=0 and c>=0):
if(a[r][c] == 1):
return 0
r = r-1
c = c-1
return 1
# Checking if right diagonal is safe
def isRightDiagonalSafe(r,c):
while(r>=0 and c<n):
if(a[r][c]==1):
return 0
r = r-1
c = c+1
return 1
def isSafe(r,c):
if(isColumnSafe(r,c) and isLeftDiagonalSafe(r,c) and isRightDiagonalSafe(r,c)):
return True
return False
def chessboard(r,c):
if(r>=n):
return
p = 0
while c<n:
p = isSafe(r,c)
if p == 1:
a[r][c] = 1
b.update({r:c})
break
c=c+1
if p==1:
chessboard(r+1,0)
else:
a[r-1][b.get(r-1)]=0
chessboard(r-1,int(b.get(r-1))+1)
chessboard(0,0)
print("Matrix is:- ",a)
print("Dictionary is:- ",b)