let find_connected cave (x,y) connected =
let rec loop seen =
let seen' = intpairset_neighbours cave seen in
if IntPairSet.is_empty seen'
then raise Not_found
else
(IntPairSet.iter
(fun (x,y) -> (if (IntPairSet.exists
(fun z -> z = (x,y)) connected)
then raise (IntPairSet.Exception (x,y))))
seen;
loop (IntPairSet.union seen seen'))
in
try
loop (IntPairSet.add (x,y) IntPairSet.empty)
with
| IntPairSet.Exception (x,y) -> (x,y)