Skip to content

Commit c24bf0c

Browse files
authored
Create Nqueen.py
1 parent 99cf7d7 commit c24bf0c

1 file changed

Lines changed: 163 additions & 0 deletions

File tree

Nqueen.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
2+
# Python program to solve N Queen
3+
# Problem using backtracking
4+
5+
6+
7+
global N
8+
9+
N = 4
10+
11+
12+
13+
def printSolution(board):
14+
15+
for i in range(N):
16+
17+
for j in range(N):
18+
19+
print board[i][j],
20+
21+
print
22+
23+
24+
25+
26+
# A utility function to check if a queen can
27+
# be placed on board[row][col]. Note that this
28+
# function is called when "col" queens are
29+
# already placed in columns from 0 to col -1.
30+
# So we need to check only left side for
31+
# attacking queens
32+
33+
def isSafe(board, row, col):
34+
35+
36+
37+
# Check this row on left side
38+
39+
for i in range(col):
40+
41+
if board[row][i] == 1:
42+
43+
return False
44+
45+
46+
47+
# Check upper diagonal on left side
48+
49+
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
50+
51+
if board[i][j] == 1:
52+
53+
return False
54+
55+
56+
57+
# Check lower diagonal on left side
58+
59+
for i, j in zip(range(row, N, 1), range(col, -1, -1)):
60+
61+
if board[i][j] == 1:
62+
63+
return False
64+
65+
66+
67+
return True
68+
69+
70+
71+
def solveNQUtil(board, col):
72+
73+
# base case: If all queens are placed
74+
75+
# then return true
76+
77+
if col >= N:
78+
79+
return True
80+
81+
82+
83+
# Consider this column and try placing
84+
85+
# this queen in all rows one by one
86+
87+
for i in range(N):
88+
89+
90+
91+
if isSafe(board, i, col):
92+
93+
# Place this queen in board[i][col]
94+
95+
board[i][col] = 1
96+
97+
98+
99+
# recur to place rest of the queens
100+
101+
if solveNQUtil(board, col + 1) == True:
102+
103+
return True
104+
105+
106+
107+
# If placing queen in board[i][col
108+
109+
# doesn't lead to a solution, then
110+
111+
# queen from board[i][col]
112+
113+
board[i][col] = 0
114+
115+
116+
117+
# if the queen can not be placed in any row in
118+
119+
# this colum col then return false
120+
121+
return False
122+
123+
124+
# This function solves the N Queen problem using
125+
# Backtracking. It mainly uses solveNQUtil() to
126+
# solve the problem. It returns false if queens
127+
# cannot be placed, otherwise return true and
128+
# placement of queens in the form of 1s.
129+
# note that there may be more than one
130+
# solutions, this function prints one of the
131+
# feasible solutions.
132+
133+
def solveNQ():
134+
135+
board = [ [0, 0, 0, 0],
136+
137+
[0, 0, 0, 0],
138+
139+
[0, 0, 0, 0],
140+
141+
[0, 0, 0, 0]
142+
143+
]
144+
145+
146+
147+
if solveNQUtil(board, 0) == False:
148+
149+
print "Solution does not exist"
150+
151+
return False
152+
153+
154+
155+
printSolution(board)
156+
157+
return True
158+
159+
160+
# driver program to test above function
161+
solveNQ()
162+
163+

0 commit comments

Comments
 (0)