-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn_queen.py
More file actions
53 lines (44 loc) · 1.33 KB
/
n_queen.py
File metadata and controls
53 lines (44 loc) · 1.33 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
def check_levels(board, m, x, y):
if m < 0:
return False
for j in range(m+1):
# print("Checking Level {}".format(j))
for k in range(len(board)):
if board[j][k] == 1 and can_attack(x,y,j,k):
# print(board[j])
return True
return False
def can_attack(x,y,a,b):
if x == a:
# print("{} {} can be attacked by {} {}".format(x, y, a, b))
return True
elif y == b:
# print("{} {} can be attacked by {} {}".format(x, y, a, b))
return True
elif abs(x-a) == abs(y-b):
# print("{} {} can be attacked by {} {}".format(x, y, a, b))
return True
# print("{} {} CANNOT be attacked by {} {}".format(x, y, a, b))
return False
def move_queen(board, i):
if i >= len(board):
print("\nSucceeded in placing queens!")
print("Board => {}\n".format(board))
return
for j in range(len(board)):
if not check_levels(board,i - 1, i,j):
board[i] = [0] * len(board)
board[i][j] = 1
# print("Board => {}".format(board))
move_queen(board, i + 1)
else:
continue
def place_queens(n=4):
board = []
for i in range(n):
t = [0]*n
board.append(t)
move_queen(board, 0)
# print(board)
def test():
place_queens()