We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 10ade5a commit e53d34dCopy full SHA for e53d34d
DP/NQueen_Dp.py
@@ -0,0 +1,21 @@
1
+#Required Sol
2
+def solveNQUtil(board, col, N):
3
+ if (col >= N):return True
4
+ for i in range(N):
5
+ if (ld[i - col + N - 1] != 1 and rd[i + col] != 1) and cl[i] != 1:
6
+ board[i][col] = 1
7
+ ld[i - col + N - 1] = rd[i + col] = cl[i] = 1
8
+ if solveNQUtil(board, col + 1, N):return True
9
+ board[i][col] = 0 # BACKTRACK
10
+ ld[i - col + N - 1] = rd[i + col] = cl[i] = 0
11
+ return False
12
+
13
+ld = [0] * 300 #LEFT DIAGONAL
14
+rd = [0] * 300 #Right diagonal
15
+cl = [0] * 300 #column check for row
16
+n = int(input())
17
+board = [[0 for col in range(n)]for row in range(n)]
18
+ans = solveNQUtil(board,0,n)
19
+if ans==False:print("No sol exist")
20
+else:
21
+ for i in range(n):print(board[i])
0 commit comments