-
Notifications
You must be signed in to change notification settings - Fork 0
/
hanoi.py
44 lines (42 loc) · 1.4 KB
/
hanoi.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
#!/usr/bin/env python
# encoding: utf-8
def hanoi1(num,left,mid,right):
RESULT = 0
if num<1:
return 0
return process(num,left,mid,right,left,right)
RESULT = 0
def process(num,left,mid,right,fr,to):
global RESULT
if num == 1:
if fr == mid or to == mid:
RESULT+=1
#print str(RESULT)+" Move 1 from "+ fr + " to "+to
return 1
else:
RESULT+=1
#print str(RESULT)+" Move 1 from "+ fr + " to "+mid
RESULT+=1
#print str(RESULT)+" Move 1 from "+ mid + " to "+to
return 2
if fr == mid or to == mid:
another = (fr == left or to == left) and right or left
part1 = process(num-1,left,mid,right,fr,another)
part2 = 1
RESULT+=1
#print str(RESULT)+" Move "+ str(num) + " from " + fr + " to "+to
part3 = process(num-1,left,mid,right,another,to)
return part1+part2+part3
else:
part1 = process(num-1,left,mid,right,fr,to)
part2 = 1
RESULT+=1
#print str(RESULT)+" Move "+ str(num) + " from " + fr + " to "+mid
part3 = process(num-1,left,mid,right,to,fr)
part4 = 1
RESULT+=1
#print str(RESULT)+" Move "+ str(num) + " from " + mid + " to "+to
part5 = process(num-1,left,mid,right,fr,to)
return part1+part2+part3+part4+part5
hanoi1(100,"left","mid","right")
print RESULT