-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChess.py
80 lines (65 loc) · 2.04 KB
/
Chess.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Description: This program outputs the number of solutions for placing n queens on an n-dimensional board
import sys
class Queens (object):
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)
# print the board
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 a position on the board is valid
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_diff = abs (col - j)
if (row_diff == col_diff) and (self.board[i][j] == 'Q'):
return False
return True
# do the recursive backtracking
def recursive_solve (self, col, sol):
if (col == self.n):
# increment sol when solution is found
# deleted return True to look for all possible solutions
sol[0] += 1
else:
for i in range (self.n):
if (self.is_valid (i, col)):
self.board[i][col] = 'Q'
# recursively checks if all columns are valid
self.recursive_solve(col + 1, sol)
# if sol not found, replace 'Q' with '*'
# if sol found, resets board back to all '*'
self.board[i][col] = '*'
# if no solution found
return False
# if the problem has a solution print the board
def solve (self):
# initializes solution num to 0
sol = [0]
self.recursive_solve(0, sol)
print(sol[0])
def main():
#read the size of the board
line = sys.stdin.readline()
line = line.strip()
n = int (line)
# create a chess board
game = Queens (n)
# place the queens on the board and count the solutions
# print the number of solutions
game.solve()
if __name__ == "__main__":
main()