-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcv.py
89 lines (76 loc) · 2.53 KB
/
lcv.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
81
82
83
84
85
86
87
88
89
import sys
import time
import numpy as np
from collections import defaultdict
BOARD_SIZE = input('Please enter Board Size: ')
start_time = time.time()
count = 0
def under_attack(col, queens):
if col in queens:
return True
else:
return bool(any(abs(col - x) == len(queens) - i for i, x in enumerate(queens)))
def LCV(queens, size):
current_row = len(queens)
constrainList = np.full((size), size - 1, dtype=np.int)
length = len(queens)
if (length == 0):
for row in range(size):
if (row - 1 != -1 and row + 1 != size):
constrainList[row] = size - 1
else:
constrainList[row] = size - 2
else:
indConstrain = 0
constrains = buildconstr(queens,size)
for cols in range(size):
if (cols not in queens and constrains[length - 1][cols] != -1):
if (cols - 1 != -1 and constrains[length][cols - 1] != -1):
indConstrain += 1
if (cols + 1 != len(queens) and constrains[length][cols - 1] != -1):
indConstrain += 1
if indConstrain > 1:
constrainList[cols] = indConstrain
else:
constrainList[cols] = size - 1
return sorted(range(len(constrainList)), key=lambda k: constrainList[k])
def buildconstr(queens, size):
track = np.zeros((size, size))
for i, j in enumerate(queens):
for x in range(size):
for y in range(size):
if (y == j):
track[x][y] = -1
if (x - y == i - j):
track[x][y] = -1
if (x + y == j - i):
track[x][y] = -1
return track
def LCV_solve(queens, n):
global count
if n == len(queens):
return queens
else:
constraints = LCV(queens, n)
for i in constraints:
if not under_attack(i, queens):
count += 1
newqueens = LCV_solve(queens + [i], n)
if newqueens != []:
return newqueens
return []
def print_board(queens):
row = 0
n = len(queens)
for pos in queens:
for i in range(pos):
sys.stdout.write(" ~ ")
sys.stdout.write(" Q ")
for i in range((n - pos) - 1):
sys.stdout.write(" ~ ")
print
ans = LCV_solve([], BOARD_SIZE)
end_time = time.time()
print_board(ans)
print("\nTime taken to complete: %s s" % (((end_time - start_time)) * 1))
print("No. of assignments: %s" % count)