-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueens.py
More file actions
77 lines (53 loc) · 1.76 KB
/
queens.py
File metadata and controls
77 lines (53 loc) · 1.76 KB
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
from select import select
from tabnanny import check
class Queens(object):
#initialize a chess board
def __init__(self, n=8):
self.board = []
self.n = n
for i in range(self.n):
row = []
for j in range(self.n):
row.append('*')
self.board.append(row)
def print_board(self):
for i in range (self.n):
for j in range (self.n):
print(self.board[i][j], end=' ')
print()
print()
# check if no queen captures another queen at that location
def is_valid(self, row, col):
for i in range(self.n):
if (self.board[row][i]=='Q') or (self.board[i][col]=='Q'):
return False
for i in range (self.n):
for j in range(self.n):
row_diff = abs(row-i)
col_dif = abs(col - j)
if(row_diff==col_dif) and (self.board[i][j]=='Q'):
return False
return True
# do a recursive backtracking
def recursive_solve(self,col):
if(col==self.n):
return True
else:
for i in range(self.n):
if(self.is_valid(i,col)):
self.board[i][col] = 'Q'
if(self.recursive_solve(col+1)):
return True
self.board[i][col] = '*'
return False
# if the problem has a solution, print the board
def solve(self):
for i in range(self.n):
if(self.recursive_solve(i)):
self.print_board()
def main():
# create a chess board
game = Queens(13)
#place the queens on the board
game.solve()
main()