File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Python/dynamic_programming Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ def checker (queens ,i ,j ):
2+ for k in queens :
3+ if k [0 ] == i :
4+ return False
5+ if k [1 ] == j :
6+ return False
7+ if abs (k [0 ]- i ) == abs (k [1 ]- j ):
8+ return False
9+ if j - 2 >= 0 and i + 1 < n :
10+ if k [0 ]== j - 2 and k [1 ]== i + 1 :
11+ return False
12+ if j - 2 >= 0 and i - 1 >= 0 :
13+ if k [0 ]== j - 2 and k [1 ]== i - 1 :
14+ return False
15+ if j - 1 >= 0 and i + 1 < n :
16+ if k [0 ]== j - 2 and k [1 ]== i + 1 :
17+ return False
18+ if j - 2 >= 0 and i + 1 < n :
19+ if k [0 ]== j - 2 and k [1 ]== i + 1 :
20+ return False
21+
22+ return True
23+
24+
25+
26+ board = []
27+ for _ in range (8 ):
28+ row = list (input ())
29+ board .append (row )
30+ # print(board)
31+
32+ queens = []
33+ n = 8
34+ count = [0 ]
35+ def main (i ):
36+ if i == n :
37+ count [0 ]+= 1
38+ return
39+ for j in range (8 ):
40+ if queens == [] and board [i ][j ]!= "*" :
41+ queens .append ([i ,j ])
42+ main (i + 1 )
43+ queens .pop ()
44+ else :
45+ if checker (queens ,i ,j ) and board [i ][j ]!= "*" :
46+ queens .append ([i ,j ])
47+ main (i + 1 )
48+ queens .pop ()
49+ main (0 )
50+ print (count [0 ])
51+
52+
53+
54+
55+
You can’t perform that action at this time.
0 commit comments