-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheightqueen.py
More file actions
66 lines (56 loc) · 1.37 KB
/
eightqueen.py
File metadata and controls
66 lines (56 loc) · 1.37 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
import math
QUEEN = 8
INITVAL = -1
def init():
""""""
arr = [INITVAL] * QUEEN
return arr
def printout(arr):
""""""
print arr
print '\n'
def valid(arr, row, col):
""""""
if row < 0 or col < 0:
return False
# col_dup = col in arr[0, row]
for i in range(0, row):
if col == arr[i] or math.fabs(row-i) == math.fabs(col-arr[i]):
return False
else:
return True
def queen(arr):
""""""
cnt, row, col = 0, 0, 0
while row < QUEEN:
while col < QUEEN:
if valid(arr, row, col):
# print row, ' and ', col
arr[row] = col
col = 0
break
else:
col = col+1
if arr[row] == INITVAL:
if row == 0:
break # fail this time
else:
row = row-1
col = arr[row]+1 # restart from col+1
arr[row] = INITVAL
continue
if row == QUEEN-1: # it can be else if other than if
cnt = cnt + 1
print 'a kind of solution'
printout(arr)
col = arr[row]+1
arr[row] = INITVAL # try next col
continue
row = row+1
print cnt
if __name__ == '__main__':
""""""
arr = init()
print arr
queen(arr)
print arr