-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
58 lines (52 loc) · 1.52 KB
/
test.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
import sys
import time
num0 = 0
def init_path(size):
allow = []
for i in range(size):
for j in range(size):
allow.append([i, j])
return allow
def get_next_choice(step, hist, raws, allow):
num = 0
for raw in raws:
nextstep = [raw[i]+step[i] for i in range(2)]
if nextstep in allow and nextstep not in hist:
num += 1
return num
def search_next(size, pos, history, allow):
nextsteps = {}
raws = [[1,2], [1,-2], [2,1], [2,-1], [-1,2], [-1,-2], [-2,1], [-2,-1]]
if len(history) == size*size:
return True
for raw in raws:
nextstep = [raw[i]+pos[i] for i in range(2)]
if nextstep in allow and nextstep not in history:
next_choice = get_next_choice(nextstep, history, raws, allow)
nextsteps[str(nextstep)] = next_choice
nextsteps = sorted(nextsteps.items(), key=lambda d:d[1])
for nextstep in nextsteps:
nextstep = list(eval(nextstep[0]))
history.append(nextstep)
back = search_next(size, nextstep, history, allow)
if back:
return True
else:
history.pop()
global num0
num0 += 1
else:
return False
def search_path(size, history):
allow = init_path(size)
position = history[-1]
back = search_next(size, position, history, allow)
if back:
return history
else:
return False
atime = time.time()
path = search_path(10, [[8,8]])
btime = time.time()
print(btime - atime)
print(num0)