Repository navigation
Expand file tree
/
Copy pathcannibals.py
More file actions
60 lines (51 loc) · 1.84 KB
/
Copy pathcannibals.py
File metadata and controls
60 lines (51 loc) · 1.84 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
C = 3
M = 3
boat = "L"
visited = []
def cross(state, path):
if state in visited:
return
if state[1] > 0 and state[0] > state[1]:
return
if M - state[1] > 0 and C - state[0] > M - state[1]:
return
if state == [0, 0, "R"]:
print("start")
for step in path:
print(step)
print("Stop\n")
return
visited.append(state[:])
if state[2] == "L":
if state[0] >= 1:
cross([state[0] - 1, state[1], "R"], path[:] + ["One cannible goes right"])
if state[0] >= 2:
cross([state[0] - 2, state[1], "R"], path[:] + ["Two cannibles go right"])
if state[0] >= 1 and state[1] >= 1:
cross(
[state[0] - 1, state[1] - 1, "R"],
path[:] + ["One cannible and one missionary go right"],
)
if state[1] >= 1:
cross(
[state[0], state[1] - 1, "R"], path[:] + ["One missionaries goes right"]
)
if state[1] >= 2:
cross(
[state[0], state[1] - 2, "R"], path[:] + ["Two missionaries go right"]
)
if state[2] == "R":
if C - state[0] >= 1:
cross([state[0] + 1, state[1], "L"], path[:] + ["One cannible goes left"])
if C - state[0] >= 2:
cross([state[0] + 2, state[1], "L"], path[:] + ["Two cannibles go left"])
if C - state[0] >= 1 and M - state[1] >= 1:
cross(
[state[0] + 1, state[1] + 1, "L"],
path[:] + ["One cannible and one missionary go left"],
)
if M - state[1] >= 1:
cross([state[0], state[1] + 1, "L"], path[:] + ["One missionary goes left"])
if M - state[1] >= 2:
cross([state[0], state[1] + 2, "L"], path[:] + ["Two missionaries go left"])
cross([3, 3, "L"], [])