-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhanoi.ml
66 lines (59 loc) · 1.64 KB
/
hanoi.ml
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
let otro ori des =
(*
Ya se que no es extausivo pero por definición:
- ori != des
- ori && des = [1,2,3]
*)
match (ori + des) with
5 -> 1 | 4 -> 2 | 3 -> 3
;;
let move (ori, des) =
(*
Ya se que no es extausivo pero por definición:
- ori != des
- ori && des = [1,2,3]
*)
let flecha ori des =
if ori > des then
match (ori + des) with
5 -> " 2 <-"
| 4 -> "<--2---"
| 3 -> "<- 2 "
else
match (ori + des) with
5 -> " 2 ->"
| 4 -> "---2-->"
| 3 -> "-> 2 "
in
" 1 " ^ flecha ori des ^ " 3 \n"
;;
let rec hanoi n ori des =
(* n número de discos, 1 <= ori <= 3, 1 <= dest <= 3, ori <> des *)
if n = 0 then "" else
let otro = otro ori des in
hanoi (n-1) ori otro ^ move (ori, des) ^ hanoi (n-1) otro des
let hanoi n ori des =
if n = 0 || ori = des then "\n"
else hanoi n ori des
let print_hanoi n ori des =
if n < 0 || ori < 1 || ori > 3 || des < 1 || des > 3
then print_endline " ** ERROR ** \n"
else print_endline (" =========== \n" ^
hanoi n ori des ^
" =========== \n")
let crono f x =
let t = Sys.time () in
f x; Sys.time () -. t
(*
let sumar (a,b,c) (d,e,f) = (a+d,b+e,c+f)
let rec nth_hanoi_move n nd ori des =
let cnt = 0 in
let rec hanoi n ori des =
if n = 0 then (1,0,0)
else if cnt = nd then (1,ori,des)
else let otro = otro ori des in
sumar (hanoi (n-1) ori otro) (hanoi (n-1) otro des)
in
hanoi nd ori des
;;
*)