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
|
test(L,E, R):-
exploration_haut(L, E, [E], M4),
exploration_bas(L, E, [E], M5),
length(M4, N4),
length(M5, N5),
( N4 > N5 -> reverse(M4, R); reverse(M5, R)).
exploration_haut(M, E, LT, L) :-
E2 is E + 1,
(E2 = 13 -> E1 = 1; E1 = E2),
select(E1, M, M1), !,
exploration_haut(M1, E1, [E1 | LT], L).
exploration_haut(M, E, LT, L) :-
E2 is E - 1,
( E2 = 0 -> E1 = 12; E1 = E2),
member(E1, M), !,
exploration_bas(M, E, LT, L).
exploration_haut(_M, _E, L, L).
exploration_bas(M, E, LT, L) :-
E2 is E - 1,
(E2 = 0 -> E1 = 12; E1 = E2),
select(E1, M, M1), !,
exploration_bas(M1, E1, [E1 | LT], L).
exploration_bas(M, E, LT, L) :-
E2 is E + 1,
( E2 = 13 -> E1 = 1; E1 = E2),
member(E1, M), !,
exploration_haut(M, E, LT, L).
exploration_bas(_M, _E, L, L).
essai :-
test([3,9,8,1,4,2, 11, 12], 2, X3),
format('Entrée ~w ~w Sortie ~w~n', [[3,9,8,1,4,2, 11, 12], 2, X3]),
writeln('*********************************'),
test([3,4,1,5,4,2,12,1,2],2,M),
format('Entrée ~w ~w Sortie ~w~n', [[3,4,1,5,4,2,12,1,2], 2, M])
. |
Partager