-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsame2020Febbraio.ml
55 lines (50 loc) · 1.36 KB
/
Esame2020Febbraio.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
type metro = (int * int * string) list
let setadd x lst =
if List.mem x lst then
lst
else
x :: lst
(* line: metro -> string -> int list *)
let line metro linea =
let rec aux tmp = function
| [] -> tmp
| (a, b, c) :: rest ->
if c = linea then
aux (setadd a (setadd b tmp)) rest
else
aux tmp rest
in
aux [] metro
(* vicini: int -> metro -> (int * string) list *)
let vicini x metro =
(* aux: metro -> (int * string) list *)
let rec aux = function
| [] -> []
| (a, b, c) :: rest ->
if a = x then
(b, c) :: aux rest
else if b = x then
(a, c) :: aux rest
else
aux rest
in
aux metro
exception NotFound
(* raggiungi: metro -> int -> int -> int -> int list *)
let raggiungi m cmax start goal =
let rec from_node visited limit last_ln (s, ln) =
if List.mem s visited || limit = 0 then
raise NotFound
else if s = goal then
[ s ]
else if last_ln != ln then
s :: from_list (s :: visited) (limit - 1) ln (vicini s m)
else
s :: from_list (s :: visited) limit ln (vicini s m)
and from_list visited limit last_ln = function
| [] -> raise NotFound
| x :: rest -> (
try from_node visited limit last_ln x
with NotFound -> from_list visited limit last_ln rest)
in
from_node [] (cmax+1) "" (start,"")