-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNQueens.py
More file actions
102 lines (84 loc) · 2.25 KB
/
NQueens.py
File metadata and controls
102 lines (84 loc) · 2.25 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import random
map = []
def createBoard():
global map
map = [
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
]
boardRef = [
["a8","b8","c8","d8","e8","f8","g8","h8"],
["a7","b7","c7","d7","e7","f7","g7","h7"],
["a6","b6","c6","d6","e6","f6","g6","h6"],
["a5","b5","c5","d5","e5","f5","g5","h5"],
["a4","b4","c4","d4","e4","f4","g4","h4"],
["a3","b3","c3","d3","e3","f3","g3","h3"],
["a2","b2","c2","d2","e2","f2","g2","h2"],
["a1","b1","c1","d1","e1","f1","g1","h1"],
]
createBoard()
queens = []
x=0
y=0
tries=0
z=1
while True:
x = random.randint(0,7)
y = random.randint(0,7)
# print("{},{}".format(x,y))
if map[x][y] == 0:
c=0
z=1
diagX = 0
diagY = 0
while c < 8:
if map[x][c] == 0:
map[x][c] = 1
if map[c][y] == 0:
map[c][y] = 1
# Up and Right
diagX = x - z
diagY = y + z
if diagX >= 0 and diagY <= 7:
if map[diagX][diagY] == 0:
map[diagX][diagY] = 1
# Up and Left
diagX = x - z
diagY = y - z
if diagX >= 0 and diagY >= 0:
if map[diagX][diagY] == 0:
map[diagX][diagY] = 1
# Down and Right
diagX = x + z
diagY = y + z
if diagX <= 7 and diagY <= 7:
if map[diagX][diagY] == 0:
map[diagX][diagY] = 1
# Down and Left
diagX = x + z
diagY = y - z
if diagX <= 7 and diagY >= 0:
if map[diagX][diagY] == 0:
map[diagX][diagY] = 1
z+=1
c+=1
map[x][y] = 2
queens.append("{}".format(boardRef[x][y]))
tries = tries + 1
if tries > 64:
if len(queens) < 9:
queens = []
createBoard()
tries=0
if len(queens) == 8:
break
for row in map:
print(row)
print(queens)
print("Placed {} queens!".format(len(queens)))