Skip to content

Commit 3c14df9

Browse files
committed
The NQueen puzzle
1 parent c98483a commit 3c14df9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Diff for: solvenqueen.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# solve the n-queens problem using recursive
2+
from queenboard import QueenBoard
3+
4+
cnt = 0
5+
def solveNQueens( board, col ):
6+
global cnt
7+
if board.numQueen() == board.size():
8+
cnt += 1
9+
print 'Placement %d: ' % cnt
10+
board.draw()
11+
#board.reset()
12+
return True
13+
else:
14+
flag = 0
15+
for row in range( board.size() ):
16+
if board.unguarded( row, col ):
17+
board.placeQueen( row, col )
18+
if solveNQueens( board, col+1 ):
19+
flag = 1
20+
board.removeQueen( row, col )
21+
if flag:
22+
return True
23+
else:
24+
return False
25+
26+
myboard = QueenBoard( 8 )
27+
solveNQueens( myboard, 0 )

0 commit comments

Comments
 (0)