/* accesseurs sur ligne et colonne*/
:-use_module(library(assoc)).


ordonne((X,Y1),(X,Y2),(X,Ym),(X,YM)):-
    YM is max(Y1,Y2),
    Ym is min(Y1,Y2),!.
ordonne((X1,Y1),(X2,Y2),(X1,Y1),(X2,Y2)):-
    X1 is min(X1,X2),!.
ordonne((X1,Y1),(X2,Y2),(X2,Y2),(X1,Y1)):-
    X2 is min(X1,X2),!.


/*rouge droite */
glissement(ro,d,Conf1,Conf2,Xr,Y):-
    gen_assoc(ro,Conf1,[1-(Xr,Y)]),
    gen_assoc(vi,Conf1,[1-(Xv,Y),2-(Xv,Yp)]),
    Xv is Xr +2,Xp is Xr+1,Yp is Y +1,
    put_assoc(ro,Conf1,[1-(Xp,Y)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xr,Y),2-(Xr,Yp)],Conf2).

/*rouge gauche */
glissement(ro,g,Conf1,Conf2,Xr,Y):-
    gen_assoc(ro,Conf1,[1-(Xr,Y)]),
    gen_assoc(vi,Conf1,[1-(Xv,Y),2-(Xv,Yp)]),
    Xv is Xr -1,Xp is Xr+1,Yp is Y +1,
    put_assoc(ro,Conf1,[1-(Xv,Y)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xp,Y),2-(Xp,Yp)],Conf2).

/*rouge bas */
glissement(ro,b,Conf1,Conf2,Xr,Y):-
    gen_assoc(ro,Conf1,[1-(Xr,Y)]),
    gen_assoc(vi,Conf1,[1-(Xr,Yv),2-(Xp,Yv)]),
    Xp is Xr +1, Yv is Y+2,Yp is Y+1,
    put_assoc(ro,Conf1,[1-(Xr,Yp)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xr,Y),2-(Xp,Y)],Conf2).

/*rouge haut */
glissement(ro,h,Conf1,Conf2,Xr,Y):-
    gen_assoc(ro,Conf1,[1-(Xr,Y)]),
    gen_assoc(vi,Conf1,[1-(Xr,Yv),2-(Xp,Yv)]),
    Yv is Y-1,Yp is Y+1,Xp is Xr +1,
    put_assoc(ro,Conf1,[1-(Xr,Yv)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xr,Yp),2-(Xp,Yp)],Conf2).

/*noir1 droite */ 
glissement(P,d,Conf1,Conf2,Xn1,Yn1):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-(Xv,Yn1),2-(Xv,Yv2)]),
    Xv is Xn1+1,Yv2 is Yn1+1,
    ordonne((Xv,Yn1),(Xn2,Yn2),No1,No2),
    put_assoc(P,Conf1,[1-No1,2-No2],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xn1,Yn1),2-(Xn1,Yv2)],Conf2).

/*noir2 droite */ 
glissement(P,d,Conf1,Conf2,Xn2,Yn2):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-(Xv,Yn2),2-(Xv,Yv2)]),
    Xv is Xn2+1,Yv2 is Yn2+1,
    put_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xv,Yn2)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xn2,Yn2),2-(Xn2,Yv2)],Conf2).

/*noir1 gauche */ 
glissement(P,g,Conf1,Conf2,Xn1,Yn1):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-(Xv,Yn1),2-(Xv,Yv2)]),
    Xv is Xn1-1,Yv2 is Yn1+1,
    put_assoc(P,Conf1,[1-(Xv,Yn1),2-(Xn2,Yn2)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xn1,Yn1),2-(Xn1,Yv2)],Conf2).

/*noir2 gauche */ 
glissement(P,g,Conf1,Conf2,Xn2,Yn2):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-(Xv,Yn2),2-(Xv,Yv2)]),
    Xv is Xn2-1,Yv2 is Yn2+1,
    ordonne((Xv,Yn2),(Xn1,Yn1),No1,No2),
    put_assoc(P,Conf1,[1-No1,2-No2],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xn2,Yn2),2-(Xn2,Yv2)],Conf2).

/*noir1 bas */ 
glissement(P,b,Conf1,Conf2,Xn1,Yn1):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-(Xn1,Yv1),2-Nv2]),
    Yn1p is Yn1+1,Yv1 is Yn1+2,
    put_assoc(P,Conf1,[1-(Xn1,Yn1p),2-(Xn2,Yn2)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xn1,Yn1),2-Nv2],Conf2).

/*noir1 bas */ 
glissement(P,b,Conf1,Conf2,Xn1,Yn1):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-Nv1,2-(Xn1,Yv1)]),
    Yn1p is Yn1+1,Yv1 is Yn1+2,
    put_assoc(P,Conf1,[1-(Xn1,Yn1p),2-(Xn2,Yn2)],Conf1b),
    put_assoc(vi,Conf1b,[1-Nv1,2-(Xn1,Yn1)],Conf2).

/*noir2 bas */ 
glissement(P,b,Conf1,Conf2,Xn2,Yn2):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-(Xn2,Yv1),2-Nv2]),
    Yn2p is Yn2+1,Yv1 is Yn2+2,
    put_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2p)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xn2,Yn2),2-Nv2],Conf2).

/*noir2 bas */ 
glissement(P,b,Conf1,Conf2,Xn2,Yn2):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-Nv1,2-(Xn2,Yv2)]),
    Yn2p is Yn2+1,Yv2 is Yn2+2,
    put_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2p)],Conf1b),
    put_assoc(vi,Conf1b,[1-Nv1,2-(Xn2,Yn2)],Conf2).

/*noir1 haut */ 
glissement(P,h,Conf1,Conf2,Xn1,Yn1):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-(Xn1,Yv1),2-Nv2]),
    Yv1p is Yn1+1,Yv1 is Yn1-1,
    put_assoc(P,Conf1,[1-(Xn1,Yv1),2-(Xn2,Yn2)],Conf1h),
    put_assoc(vi,Conf1h,[1-(Xn1,Yv1p),2-Nv2],Conf2).

/*noir1 haut */ 
glissement(P,h,Conf1,Conf2,Xn1,Yn1):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-Nv1,2-(Xn1,Yv1)]),
    Yv1p is Yn1+1,Yv1 is Yn1-1,
    put_assoc(P,Conf1,[1-(Xn1,Yv1),2-(Xn2,Yn2)],Conf1h),
    put_assoc(vi,Conf1h,[1-Nv1,2-(Xn1,Yv1p)],Conf2).

/*noir2 haut */ 
glissement(P,h,Conf1,Conf2,Xn2,Yn2):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-(Xn2,Yv1),2-Nv2]),
    Yv1p is Yn2+1,Yv1 is Yn2-1,
    put_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yv1)],Conf1h),
    put_assoc(vi,Conf1h,[1-(Xn2,Yv1p),2-Nv2],Conf2).

/*noir2 haut */ 
glissement(P,h,Conf1,Conf2,Xn2,Yn2):-
    (P=no;P=ja),
    gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
    gen_assoc(vi,Conf1,[1-Nv1,2-(Xn2,Yv2)]),
    Yv2p is Yn2+1,Yv2 is Yn2-1,
    put_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yv2)],Conf1h),
    put_assoc(vi,Conf1h,[1-Nv1,2-(Xn2,Yv2p)],Conf2).

/*blanc droite */
glissement(bl,d,Conf1,Conf2,Xb,Yb):-
    gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
    gen_assoc(vi,Conf1,[1-(Xv1,Yb),2-Nv2]),
    Xv1 is Xb +2,Xp is Xb+1,
    put_assoc(bl,Conf1,[1-(Xp,Yb)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xb,Yb),2-Nv2],Conf2).

/*blanc droite */
glissement(bl,d,Conf1,Conf2,Xb,Yb):-
    gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
    gen_assoc(vi,Conf1,[1-(Xv1,Yv1),2-(Xv2,Yb)]),
    Xv2 is Xb +2,Xp is Xb+1,
    put_assoc(bl,Conf1,[1-(Xp,Yb)],Conf1b),
    ordonne((Xv1,Yv1),(Xb,Yb),Nv1,Nv2),
    put_assoc(vi,Conf1b,[1-Nv1,2-Nv2],Conf2).


/*blanc gauche */
glissement(bl,g,Conf1,Conf2,Xb,Yb):-
    gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
    gen_assoc(vi,Conf1,[1-(Xv1,Yb),2-V2]),
    Xv1 is Xb-1,Xp is Xb+1,
    ordonne((Xp,Yb),V2,V1p,V2p),
    put_assoc(bl,Conf1,[1-(Xv1,Yb)],Conf1b),
    put_assoc(vi,Conf1b,[1-V1p,2-V2p],Conf2).
/*    write('blanc gauche : '),writeln(Conf2).*/

/*blanc gauche */
glissement(bl,g,Conf1,Conf2,Xb,Yb):-
    gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
    gen_assoc(vi,Conf1,[1-V1,2-(Xv2,Yb)]),
    Xv2 is Xb-1,Xp is Xb+1,
    put_assoc(bl,Conf1,[1-(Xv2,Yb)],Conf1b),
    put_assoc(vi,Conf1b,[1-V1,2-(Xp,Yb)],Conf2).
/*    write('blanc gauche : '),writeln(Conf2).*/




/*blanc bas */
glissement(bl,b,Conf1,Conf2,Xb,Yb):-
    gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
    gen_assoc(vi,Conf1,[1-(Xb,Yv1),2-(Xv2,Yv1)]),
    Yv1 is Yb+1, Xv2 is Xb +1,
    put_assoc(bl,Conf1,[1-(Xb,Yv1)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xb,Yb),2-(Xv2,Yb)],Conf2).

/*blanc haut */
glissement(bl,h,Conf1,Conf2,Xb,Yb):-
    gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
    gen_assoc(vi,Conf1,[1-(Xb,Yv1),2-(Xv2,Yv1)]),
    Yv1 is Yb-1, Xv2 is Xb +1,
    put_assoc(bl,Conf1,[1-(Xb,Yv1)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xb,Yb),2-(Xv2,Yb)],Conf2).

/*ma1 droite */
glissement(P,d,Conf1,Conf2,Xm1,Ym1):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-(Xm2,Ym2)]),
    gen_assoc(vi,Conf1,[1-(Xv1,Ym1),2-Nv2]),
    Xv1 is Xm1 +1,
    ordonne((Xv1,Ym1),(Xm2,Ym2),Nm1,Nm2),
    put_assoc(P,Conf1,[1-Nm1,2-Nm2],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xm1,Ym1),2-Nv2],Conf2).

/*ma1 droite */
glissement(P,d,Conf1,Conf2,Xm1,Ym1):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-(Xm2,Ym2)]),
    gen_assoc(vi,Conf1,[1-(Xv1,Yv1),2-(Xv2,Ym1)]),
    Xv2 is Xm1 +1,
    ordonne((Xv2,Ym1),(Xm2,Ym2),Nm1,Nm2),
    put_assoc(P,Conf1,[1-Nm1,2-Nm2],Conf1b),
    ordonne((Xv1,Yv1),(Xm1,Ym1),Nv1,Nv2),
    put_assoc(vi,Conf1b,[1-Nv1,2-Nv2],Conf2).

/*ma2 droite */
glissement(P,d,Conf1,Conf2,Xm2,Ym2):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-(Xm2,Ym2)]),
    gen_assoc(vi,Conf1,[1-(Xv1,Ym2),2-Nv2]),
    Xv1 is Xm2 +1,
    put_assoc(P,Conf1,[1-(Xm1,Ym1),2-(Xv1,Ym2)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xm2,Ym2),2-Nv2],Conf2).

/*ma2 droite */
glissement(P,d,Conf1,Conf2,Xm2,Ym2):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
    gen_assoc(vi,Conf1,[1-V1,2-(Xv2,Ym2)]),
    Xv2 is Xm2 +1,
    put_assoc(P,Conf1,[1-M1,2-(Xv2,Ym2)],Conf1b),
    ordonne(V1,(Xm2,Ym2),Nv1,Nv2),
    put_assoc(vi,Conf1b,[1-Nv1,2-Nv2],Conf2).
    

/*ma1 gauche */
glissement(P,g,Conf1,Conf2,Xm1,Ym1):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
    gen_assoc(vi,Conf1,[1-(Xv1,Ym1),2-Nv2]),
    Xv1 is Xm1 -1,
    put_assoc(P,Conf1,[1-(Xv1,Ym1),2-M2],Conf1b),
    ordonne((Xm1,Ym1),Nv2,Nv1p,Nv2p),
    put_assoc(vi,Conf1b,[1-Nv1p,2-Nv2p],Conf2).

/*ma1 gauche */
glissement(P,g,Conf1,Conf2,Xm1,Ym1):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
    gen_assoc(vi,Conf1,[1-V1,2-(Xv2,Ym1)]),
    Xv2 is Xm1 -1,
    put_assoc(P,Conf1,[1-(Xv2,Ym1),2-M2],Conf1b),
    put_assoc(vi,Conf1b,[1-V1,2-(Xm1,Ym1)],Conf2).

/*ma2 gauche */
glissement(P,g,Conf1,Conf2,Xm2,Ym2):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
    gen_assoc(vi,Conf1,[1-(Xv1,Ym2),2-V2]),
    Xv1 is Xm2 -1,
    ordonne(M1,(Xv1,Ym2),M1p,M2p),
    put_assoc(P,Conf1,[1-M1p,2-M2p],Conf1b),
    ordonne((Xm2,Ym2),V2,V1p,V2p),
    put_assoc(vi,Conf1b,[1-V1p,2-V2p],Conf2).

/*ma2 gauche */
glissement(P,g,Conf1,Conf2,Xm2,Ym2):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
    gen_assoc(vi,Conf1,[1-V1,2-(Xv2,Ym2)]),
    Xv2 is Xm2 -1,
    ordonne(M1,(Xv2,Ym2),M1p,M2p),
    put_assoc(P,Conf1,[1-M1p,2-M2p],Conf1b),
    put_assoc(vi,Conf1b,[1-V1,2-(Xm2,Ym2)],Conf2).


/*ma1 bas */
glissement(P,b,Conf1,Conf2,Xm1,Ym1):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
    gen_assoc(vi,Conf1,[1-(Xm1,Yv1),2-Nv2]),
    Yv1 is Ym1 +1,
    put_assoc(P,Conf1,[1-(Xm1,Yv1),2-M2],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xm1,Ym1),2-Nv2],Conf2).

/*ma1 bas */
glissement(P,b,Conf1,Conf2,Xm1,Ym1):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
    gen_assoc(vi,Conf1,[1-V1,2-(Xm1,Yv2)]),
    Yv2 is Ym1 +1,
    put_assoc(P,Conf1,[1-(Xm1,Yv2),2-M2],Conf1b),
    put_assoc(vi,Conf1b,[1-V1,2-(Xm1,Ym1)],Conf2).

/*ma2 bas */
glissement(P,b,Conf1,Conf2,Xm2,Ym2):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
    gen_assoc(vi,Conf1,[1-(Xm2,Yv1),2-V2]),
    Yv1 is Ym2 +1,
    put_assoc(P,Conf1,[1-M1,2-(Xm2,Yv1)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xm2,Ym2),2-V2],Conf2).

/*ma2 bas */
glissement(P,b,Conf1,Conf2,Xm2,Ym2):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
    gen_assoc(vi,Conf1,[1-V1,2-(Xm2,Yv2)]),
    Yv2 is Ym2 +1,
    put_assoc(P,Conf1,[1-M1,2-(Xm2,Yv2)],Conf1b),
    put_assoc(vi,Conf1b,[1-V1,2-(Xm2,Ym2)],Conf2).
    
/*ma1 haut */
glissement(P,h,Conf1,Conf2,Xm1,Ym1):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
    gen_assoc(vi,Conf1,[1-(Xm1,Yv1),2-V2]),
    Yv1 is Ym1 -1,
    put_assoc(P,Conf1,[1-(Xm1,Yv1),2-M2],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xm1,Ym1),2-V2],Conf2).

/*ma1 haut */
glissement(P,h,Conf1,Conf2,Xm1,Ym1):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
    gen_assoc(vi,Conf1,[1-V1,2-(Xm1,Yv2)]),
    Yv2 is Ym1 -1,
    put_assoc(P,Conf1,[1-(Xm1,Yv2),2-M2],Conf1b),
    put_assoc(vi,Conf1b,[1-V1,2-(Xm1,Ym1)],Conf2).

/*ma2 haut */
glissement(P,h,Conf1,Conf2,Xm2,Ym2):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
    gen_assoc(vi,Conf1,[1-(Xm2,Yv1),2-V2]),
    Yv1 is Ym2 -1,
    put_assoc(P,Conf1,[1-M1,2-(Xm2,Yv1)],Conf1b),
    put_assoc(vi,Conf1b,[1-(Xm2,Ym2),2-V2],Conf2).

/*ma2 haut */
glissement(P,h,Conf1,Conf2,Xm2,Ym2):-
    (P=ma;P=bo),
    gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
    gen_assoc(vi,Conf1,[1-V1,2-(Xm2,Yv2)]),
    Yv2 is Ym2 -1,
    put_assoc(P,Conf1,[1-M1,2-(Xm2,Yv2)],Conf1b),
    put_assoc(vi,Conf1b,[1-V1,2-(Xm2,Ym2)],Conf2).

retour(L,Col,d,L,Cp,g):-
    Col is Cp-1.
retour(L,Col,g,L,Cp,d):-
    Col is Cp+1.
retour(Lig,C,h,Lp,C,b):-
    Lig is Lp+1.
    retour(Lig,C,b,Lp,C,h):-
    Lig is Lp-1.


gl_time(P,D,Confa,Confb):-
    get_time(T2),
    glissement(P,D,Confa,Confb),
    get_time(T3),
    DT3 is T3 - T2,
    write('depl : '),writeln(DT3).
    

gl_ok(P,D,Confa,Confb,Lig,Col,[]):-
    glissement(P,D,Confa,Confb,Lig,Col).
gl_ok(_,D,Confa,Confb,Lig,Col,[((Lp,Cp),Dp)|_]):-
    glissement(_,D,Confa,Confb,Lig,Col),
    not(retour(Lig,Col,D,Lp,Cp,Dp)).


    /* Tous les successeurs d une configuration */
successeurs(Confa-Chemins,L):-
    findall(
	Confb-[((Lig,Col),D)|Chemins],
	/*gl_ok(_,D,Confa,Confb,Lig,Col,Chemins),*/
	glissement(_,D,Confa,Confb,Lig,Col),
	L).

successeurs_liste(L,Sl):-
    maplist(successeurs,L,Lp),
    flatten(Lp,Lpp),
    list_to_assoc(Lpp,A),
    assoc_to_list(A,Sl).


 
/* regarde si le premier parametre appartient 
     à la liste donné en second parametre. Si c est le cas,
    retourne le chemin */






appartient(Conf1-_,[Conf1-_|_]):-!.
appartient(Conf1-C,[_|L]):-
    appartient(Conf1-C,L).


difference([],_,[]).
difference([El|L1],L2,Res):-
    appartient(El,L2),
    !,
    difference(L1,L2,Res).
difference([El|L1],L2,[El|Res]):-
    difference(L1,L2,Res).






but([Conf-Chemin|_],Chemin):-final(Conf),
    write(Conf),!.
but([_|L],Chemin):-but(L,Chemin).


largeur(Atraiter,_,_,Res):-
    but(Atraiter,Res),
    !.
largeur(Atraiter,Visites,C,Res):-
    Cp is C+1,
    write('boucle'), writeln(C),
    get_time(T0),
    successeurs_liste(Atraiter,Succ),
    length(Succ,Taille),
    write('taille succ : '),writeln(Taille),
    get_time(T1),
    DT1 is T1 - T0,
    append(Atraiter,Visites,Visites2),
    get_time(T2),
    difference(Succ,Visites2,Atraiter2),
    get_time(T3),
    DT3 is T3 - T2,
    write('successeur : '),writeln(DT1),
    write('difference : '),writeln(DT3),
    largeur(Atraiter2,Visites2,Cp,Res).

resoud(Res):-
    init(X),
    largeur([X-[]],[],0,Res).


/*trad(El-L,El-A):-
    list_to_assoc(L,A).
 */   
    
init(X):-
    list_to_assoc(
	    [
	     ja-[1-(0,0),2-(3,0)],       
	     ro-[1-(1,0)],
	     no-[1-(0,2),2-(3,2)],
	     bl-[1-(1,2)],
	     ma-[1-(0,4),2-(3,4)],
	     bo-[1-(1,3),2-(2,3)],
	     vi-[1-(1,4),2-(2,4)]],
	    X).
/*
final(X):-    
    gen_assoc(no,X,[1-(1,3),2-(3,3)]),
    gen_assoc(bl,X,[1-(1,2)]),
    gen_assoc(ma,X,[1-(0,3),_]).
*/
final(X):-    
    gen_assoc(no,X,[1-(1,3),2-(2,3)]).

     

/*
final(X):-
    gen_assoc(ja,X,[1-(0,0),2-(3,0)]),       
    gen_assoc(ro,X,[1-(1,0)]),
    gen_assoc(no,X,[1-(1,3),2-(3,3)]),
    gen_assoc(bl,X,[1-(0,2)]),
    gen_assoc(ma,X,[1-(0,3),2-(2,4)]),
    gen_assoc(bo,X,[1-(0,4),2-(3,2)]).

*/


