Построить фигуру не отрывая письменной принадлежности от бумаги не про

 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
{-
Построить фигуру, не отрывая письменной принадлежности от бумаги и не проводя одну и ту же линию дважды
1
/ \
2---3
/|\ /|\
4 | 5 | 6
\|/ \|/
7---8
\ /
9
-}
ps = [ (1,2)
, (1,3)
, (2,3)
, (2,4)
, (2,5)
, (2,7)
, (3,5)
, (3,6)
, (3,8)
, (4,7)
, (5,7)
, (5,8)
, (6,8)
, (7,8)
, (7,9)
, (8,9) ]
getLongest (x:[]) = x
getLongest (x:xs) =
let y = getLongest xs in
if (length x) > (length y) then x else y
possibleMoves x = [fst m | m <- ps, (snd m) == x] ++ [snd m | m <- ps, (fst m) == x]
makeMove x y = if (x < y) then (x,y) else (y,x)
makeMovesList :: (Integral a) => [a] -> [(a,a)]
makeMovesList (x:y:ys) = [makeMove x y] ++ (makeMovesList (y:ys))
makeMovesList _ = []
makePath [] = getLongest [path | path <- [makePath [x] | x <- [1..9]]]
makePath xs =
let xt = (last xs) in
let ps = [x | x <- (possibleMoves xt), not ((makeMove x xt) `elem` (makeMovesList xs))] in -- ps — все возможные ходы, исключая уже сделанные
if (null ps)
then xs
else (getLongest [makePath (xs ++ [p]) | p <- ps])
main = print (makePath [])