1 /* accesseurs sur ligne et colonne*/
2 :-use_module(library(assoc)).
5 ordonne((X,Y1),(X,Y2),(X,Ym),(X,YM)):-
8 ordonne((X1,Y1),(X2,Y2),(X1,Y1),(X2,Y2)):-
10 ordonne((X1,Y1),(X2,Y2),(X2,Y2),(X1,Y1)):-
15 glissement(ro,d,Conf1,Conf2,Xr,Y):-
16 gen_assoc(ro,Conf1,[1-(Xr,Y)]),
17 gen_assoc(vi,Conf1,[1-(Xv,Y),2-(Xv,Yp)]),
18 Xv is Xr +2,Xp is Xr+1,Yp is Y +1,
19 put_assoc(ro,Conf1,[1-(Xp,Y)],Conf1b),
20 put_assoc(vi,Conf1b,[1-(Xr,Y),2-(Xr,Yp)],Conf2).
23 glissement(ro,g,Conf1,Conf2,Xr,Y):-
24 gen_assoc(ro,Conf1,[1-(Xr,Y)]),
25 gen_assoc(vi,Conf1,[1-(Xv,Y),2-(Xv,Yp)]),
26 Xv is Xr -1,Xp is Xr+1,Yp is Y +1,
27 put_assoc(ro,Conf1,[1-(Xv,Y)],Conf1b),
28 put_assoc(vi,Conf1b,[1-(Xp,Y),2-(Xp,Yp)],Conf2).
31 glissement(ro,b,Conf1,Conf2,Xr,Y):-
32 gen_assoc(ro,Conf1,[1-(Xr,Y)]),
33 gen_assoc(vi,Conf1,[1-(Xr,Yv),2-(Xp,Yv)]),
34 Xp is Xr +1, Yv is Y+2,Yp is Y+1,
35 put_assoc(ro,Conf1,[1-(Xr,Yp)],Conf1b),
36 put_assoc(vi,Conf1b,[1-(Xr,Y),2-(Xp,Y)],Conf2).
39 glissement(ro,h,Conf1,Conf2,Xr,Y):-
40 gen_assoc(ro,Conf1,[1-(Xr,Y)]),
41 gen_assoc(vi,Conf1,[1-(Xr,Yv),2-(Xp,Yv)]),
42 Yv is Y-1,Yp is Y+1,Xp is Xr +1,
43 put_assoc(ro,Conf1,[1-(Xr,Yv)],Conf1b),
44 put_assoc(vi,Conf1b,[1-(Xr,Yp),2-(Xp,Yp)],Conf2).
47 glissement(P,d,Conf1,Conf2,Xn1,Yn1):-
49 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
50 gen_assoc(vi,Conf1,[1-(Xv,Yn1),2-(Xv,Yv2)]),
51 Xv is Xn1+1,Yv2 is Yn1+1,
52 ordonne((Xv,Yn1),(Xn2,Yn2),No1,No2),
53 put_assoc(P,Conf1,[1-No1,2-No2],Conf1b),
54 put_assoc(vi,Conf1b,[1-(Xn1,Yn1),2-(Xn1,Yv2)],Conf2).
57 glissement(P,d,Conf1,Conf2,Xn2,Yn2):-
59 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
60 gen_assoc(vi,Conf1,[1-(Xv,Yn2),2-(Xv,Yv2)]),
61 Xv is Xn2+1,Yv2 is Yn2+1,
62 put_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xv,Yn2)],Conf1b),
63 put_assoc(vi,Conf1b,[1-(Xn2,Yn2),2-(Xn2,Yv2)],Conf2).
66 glissement(P,g,Conf1,Conf2,Xn1,Yn1):-
68 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
69 gen_assoc(vi,Conf1,[1-(Xv,Yn1),2-(Xv,Yv2)]),
70 Xv is Xn1-1,Yv2 is Yn1+1,
71 put_assoc(P,Conf1,[1-(Xv,Yn1),2-(Xn2,Yn2)],Conf1b),
72 put_assoc(vi,Conf1b,[1-(Xn1,Yn1),2-(Xn1,Yv2)],Conf2).
75 glissement(P,g,Conf1,Conf2,Xn2,Yn2):-
77 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
78 gen_assoc(vi,Conf1,[1-(Xv,Yn2),2-(Xv,Yv2)]),
79 Xv is Xn2-1,Yv2 is Yn2+1,
80 ordonne((Xv,Yn2),(Xn1,Yn1),No1,No2),
81 put_assoc(P,Conf1,[1-No1,2-No2],Conf1b),
82 put_assoc(vi,Conf1b,[1-(Xn2,Yn2),2-(Xn2,Yv2)],Conf2).
85 glissement(P,b,Conf1,Conf2,Xn1,Yn1):-
87 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
88 gen_assoc(vi,Conf1,[1-(Xn1,Yv1),2-Nv2]),
89 Yn1p is Yn1+1,Yv1 is Yn1+2,
90 put_assoc(P,Conf1,[1-(Xn1,Yn1p),2-(Xn2,Yn2)],Conf1b),
91 put_assoc(vi,Conf1b,[1-(Xn1,Yn1),2-Nv2],Conf2).
94 glissement(P,b,Conf1,Conf2,Xn1,Yn1):-
96 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
97 gen_assoc(vi,Conf1,[1-Nv1,2-(Xn1,Yv1)]),
98 Yn1p is Yn1+1,Yv1 is Yn1+2,
99 put_assoc(P,Conf1,[1-(Xn1,Yn1p),2-(Xn2,Yn2)],Conf1b),
100 put_assoc(vi,Conf1b,[1-Nv1,2-(Xn1,Yn1)],Conf2).
103 glissement(P,b,Conf1,Conf2,Xn2,Yn2):-
105 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
106 gen_assoc(vi,Conf1,[1-(Xn2,Yv1),2-Nv2]),
107 Yn2p is Yn2+1,Yv1 is Yn2+2,
108 put_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2p)],Conf1b),
109 put_assoc(vi,Conf1b,[1-(Xn2,Yn2),2-Nv2],Conf2).
112 glissement(P,b,Conf1,Conf2,Xn2,Yn2):-
114 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
115 gen_assoc(vi,Conf1,[1-Nv1,2-(Xn2,Yv2)]),
116 Yn2p is Yn2+1,Yv2 is Yn2+2,
117 put_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2p)],Conf1b),
118 put_assoc(vi,Conf1b,[1-Nv1,2-(Xn2,Yn2)],Conf2).
121 glissement(P,h,Conf1,Conf2,Xn1,Yn1):-
123 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
124 gen_assoc(vi,Conf1,[1-(Xn1,Yv1),2-Nv2]),
125 Yv1p is Yn1+1,Yv1 is Yn1-1,
126 put_assoc(P,Conf1,[1-(Xn1,Yv1),2-(Xn2,Yn2)],Conf1h),
127 put_assoc(vi,Conf1h,[1-(Xn1,Yv1p),2-Nv2],Conf2).
130 glissement(P,h,Conf1,Conf2,Xn1,Yn1):-
132 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
133 gen_assoc(vi,Conf1,[1-Nv1,2-(Xn1,Yv1)]),
134 Yv1p is Yn1+1,Yv1 is Yn1-1,
135 put_assoc(P,Conf1,[1-(Xn1,Yv1),2-(Xn2,Yn2)],Conf1h),
136 put_assoc(vi,Conf1h,[1-Nv1,2-(Xn1,Yv1p)],Conf2).
139 glissement(P,h,Conf1,Conf2,Xn2,Yn2):-
141 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
142 gen_assoc(vi,Conf1,[1-(Xn2,Yv1),2-Nv2]),
143 Yv1p is Yn2+1,Yv1 is Yn2-1,
144 put_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yv1)],Conf1h),
145 put_assoc(vi,Conf1h,[1-(Xn2,Yv1p),2-Nv2],Conf2).
148 glissement(P,h,Conf1,Conf2,Xn2,Yn2):-
150 gen_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yn2)]),
151 gen_assoc(vi,Conf1,[1-Nv1,2-(Xn2,Yv2)]),
152 Yv2p is Yn2+1,Yv2 is Yn2-1,
153 put_assoc(P,Conf1,[1-(Xn1,Yn1),2-(Xn2,Yv2)],Conf1h),
154 put_assoc(vi,Conf1h,[1-Nv1,2-(Xn2,Yv2p)],Conf2).
157 glissement(bl,d,Conf1,Conf2,Xb,Yb):-
158 gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
159 gen_assoc(vi,Conf1,[1-(Xv1,Yb),2-Nv2]),
160 Xv1 is Xb +2,Xp is Xb+1,
161 put_assoc(bl,Conf1,[1-(Xp,Yb)],Conf1b),
162 put_assoc(vi,Conf1b,[1-(Xb,Yb),2-Nv2],Conf2).
165 glissement(bl,d,Conf1,Conf2,Xb,Yb):-
166 gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
167 gen_assoc(vi,Conf1,[1-(Xv1,Yv1),2-(Xv2,Yb)]),
168 Xv2 is Xb +2,Xp is Xb+1,
169 put_assoc(bl,Conf1,[1-(Xp,Yb)],Conf1b),
170 ordonne((Xv1,Yv1),(Xb,Yb),Nv1,Nv2),
171 put_assoc(vi,Conf1b,[1-Nv1,2-Nv2],Conf2).
175 glissement(bl,g,Conf1,Conf2,Xb,Yb):-
176 gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
177 gen_assoc(vi,Conf1,[1-(Xv1,Yb),2-V2]),
178 Xv1 is Xb-1,Xp is Xb+1,
179 ordonne((Xp,Yb),V2,V1p,V2p),
180 put_assoc(bl,Conf1,[1-(Xv1,Yb)],Conf1b),
181 put_assoc(vi,Conf1b,[1-V1p,2-V2p],Conf2).
182 /* write('blanc gauche : '),writeln(Conf2).*/
185 glissement(bl,g,Conf1,Conf2,Xb,Yb):-
186 gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
187 gen_assoc(vi,Conf1,[1-V1,2-(Xv2,Yb)]),
188 Xv2 is Xb-1,Xp is Xb+1,
189 put_assoc(bl,Conf1,[1-(Xv2,Yb)],Conf1b),
190 put_assoc(vi,Conf1b,[1-V1,2-(Xp,Yb)],Conf2).
191 /* write('blanc gauche : '),writeln(Conf2).*/
197 glissement(bl,b,Conf1,Conf2,Xb,Yb):-
198 gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
199 gen_assoc(vi,Conf1,[1-(Xb,Yv1),2-(Xv2,Yv1)]),
200 Yv1 is Yb+1, Xv2 is Xb +1,
201 put_assoc(bl,Conf1,[1-(Xb,Yv1)],Conf1b),
202 put_assoc(vi,Conf1b,[1-(Xb,Yb),2-(Xv2,Yb)],Conf2).
205 glissement(bl,h,Conf1,Conf2,Xb,Yb):-
206 gen_assoc(bl,Conf1,[1-(Xb,Yb)]),
207 gen_assoc(vi,Conf1,[1-(Xb,Yv1),2-(Xv2,Yv1)]),
208 Yv1 is Yb-1, Xv2 is Xb +1,
209 put_assoc(bl,Conf1,[1-(Xb,Yv1)],Conf1b),
210 put_assoc(vi,Conf1b,[1-(Xb,Yb),2-(Xv2,Yb)],Conf2).
213 glissement(P,d,Conf1,Conf2,Xm1,Ym1):-
215 gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-(Xm2,Ym2)]),
216 gen_assoc(vi,Conf1,[1-(Xv1,Ym1),2-Nv2]),
218 ordonne((Xv1,Ym1),(Xm2,Ym2),Nm1,Nm2),
219 put_assoc(P,Conf1,[1-Nm1,2-Nm2],Conf1b),
220 put_assoc(vi,Conf1b,[1-(Xm1,Ym1),2-Nv2],Conf2).
223 glissement(P,d,Conf1,Conf2,Xm1,Ym1):-
225 gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-(Xm2,Ym2)]),
226 gen_assoc(vi,Conf1,[1-(Xv1,Yv1),2-(Xv2,Ym1)]),
228 ordonne((Xv2,Ym1),(Xm2,Ym2),Nm1,Nm2),
229 put_assoc(P,Conf1,[1-Nm1,2-Nm2],Conf1b),
230 ordonne((Xv1,Yv1),(Xm1,Ym1),Nv1,Nv2),
231 put_assoc(vi,Conf1b,[1-Nv1,2-Nv2],Conf2).
234 glissement(P,d,Conf1,Conf2,Xm2,Ym2):-
236 gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-(Xm2,Ym2)]),
237 gen_assoc(vi,Conf1,[1-(Xv1,Ym2),2-Nv2]),
239 put_assoc(P,Conf1,[1-(Xm1,Ym1),2-(Xv1,Ym2)],Conf1b),
240 put_assoc(vi,Conf1b,[1-(Xm2,Ym2),2-Nv2],Conf2).
243 glissement(P,d,Conf1,Conf2,Xm2,Ym2):-
245 gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
246 gen_assoc(vi,Conf1,[1-V1,2-(Xv2,Ym2)]),
248 put_assoc(P,Conf1,[1-M1,2-(Xv2,Ym2)],Conf1b),
249 ordonne(V1,(Xm2,Ym2),Nv1,Nv2),
250 put_assoc(vi,Conf1b,[1-Nv1,2-Nv2],Conf2).
254 glissement(P,g,Conf1,Conf2,Xm1,Ym1):-
256 gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
257 gen_assoc(vi,Conf1,[1-(Xv1,Ym1),2-Nv2]),
259 put_assoc(P,Conf1,[1-(Xv1,Ym1),2-M2],Conf1b),
260 ordonne((Xm1,Ym1),Nv2,Nv1p,Nv2p),
261 put_assoc(vi,Conf1b,[1-Nv1p,2-Nv2p],Conf2).
264 glissement(P,g,Conf1,Conf2,Xm1,Ym1):-
266 gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
267 gen_assoc(vi,Conf1,[1-V1,2-(Xv2,Ym1)]),
269 put_assoc(P,Conf1,[1-(Xv2,Ym1),2-M2],Conf1b),
270 put_assoc(vi,Conf1b,[1-V1,2-(Xm1,Ym1)],Conf2).
273 glissement(P,g,Conf1,Conf2,Xm2,Ym2):-
275 gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
276 gen_assoc(vi,Conf1,[1-(Xv1,Ym2),2-V2]),
278 ordonne(M1,(Xv1,Ym2),M1p,M2p),
279 put_assoc(P,Conf1,[1-M1p,2-M2p],Conf1b),
280 ordonne((Xm2,Ym2),V2,V1p,V2p),
281 put_assoc(vi,Conf1b,[1-V1p,2-V2p],Conf2).
284 glissement(P,g,Conf1,Conf2,Xm2,Ym2):-
286 gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
287 gen_assoc(vi,Conf1,[1-V1,2-(Xv2,Ym2)]),
289 ordonne(M1,(Xv2,Ym2),M1p,M2p),
290 put_assoc(P,Conf1,[1-M1p,2-M2p],Conf1b),
291 put_assoc(vi,Conf1b,[1-V1,2-(Xm2,Ym2)],Conf2).
295 glissement(P,b,Conf1,Conf2,Xm1,Ym1):-
297 gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
298 gen_assoc(vi,Conf1,[1-(Xm1,Yv1),2-Nv2]),
300 put_assoc(P,Conf1,[1-(Xm1,Yv1),2-M2],Conf1b),
301 put_assoc(vi,Conf1b,[1-(Xm1,Ym1),2-Nv2],Conf2).
304 glissement(P,b,Conf1,Conf2,Xm1,Ym1):-
306 gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
307 gen_assoc(vi,Conf1,[1-V1,2-(Xm1,Yv2)]),
309 put_assoc(P,Conf1,[1-(Xm1,Yv2),2-M2],Conf1b),
310 put_assoc(vi,Conf1b,[1-V1,2-(Xm1,Ym1)],Conf2).
313 glissement(P,b,Conf1,Conf2,Xm2,Ym2):-
315 gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
316 gen_assoc(vi,Conf1,[1-(Xm2,Yv1),2-V2]),
318 put_assoc(P,Conf1,[1-M1,2-(Xm2,Yv1)],Conf1b),
319 put_assoc(vi,Conf1b,[1-(Xm2,Ym2),2-V2],Conf2).
322 glissement(P,b,Conf1,Conf2,Xm2,Ym2):-
324 gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
325 gen_assoc(vi,Conf1,[1-V1,2-(Xm2,Yv2)]),
327 put_assoc(P,Conf1,[1-M1,2-(Xm2,Yv2)],Conf1b),
328 put_assoc(vi,Conf1b,[1-V1,2-(Xm2,Ym2)],Conf2).
331 glissement(P,h,Conf1,Conf2,Xm1,Ym1):-
333 gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
334 gen_assoc(vi,Conf1,[1-(Xm1,Yv1),2-V2]),
336 put_assoc(P,Conf1,[1-(Xm1,Yv1),2-M2],Conf1b),
337 put_assoc(vi,Conf1b,[1-(Xm1,Ym1),2-V2],Conf2).
340 glissement(P,h,Conf1,Conf2,Xm1,Ym1):-
342 gen_assoc(P,Conf1,[1-(Xm1,Ym1),2-M2]),
343 gen_assoc(vi,Conf1,[1-V1,2-(Xm1,Yv2)]),
345 put_assoc(P,Conf1,[1-(Xm1,Yv2),2-M2],Conf1b),
346 put_assoc(vi,Conf1b,[1-V1,2-(Xm1,Ym1)],Conf2).
349 glissement(P,h,Conf1,Conf2,Xm2,Ym2):-
351 gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
352 gen_assoc(vi,Conf1,[1-(Xm2,Yv1),2-V2]),
354 put_assoc(P,Conf1,[1-M1,2-(Xm2,Yv1)],Conf1b),
355 put_assoc(vi,Conf1b,[1-(Xm2,Ym2),2-V2],Conf2).
358 glissement(P,h,Conf1,Conf2,Xm2,Ym2):-
360 gen_assoc(P,Conf1,[1-M1,2-(Xm2,Ym2)]),
361 gen_assoc(vi,Conf1,[1-V1,2-(Xm2,Yv2)]),
363 put_assoc(P,Conf1,[1-M1,2-(Xm2,Yv2)],Conf1b),
364 put_assoc(vi,Conf1b,[1-V1,2-(Xm2,Ym2)],Conf2).
366 retour(L,Col,d,L,Cp,g):-
368 retour(L,Col,g,L,Cp,d):-
370 retour(Lig,C,h,Lp,C,b):-
372 retour(Lig,C,b,Lp,C,h):-
376 gl_time(P,D,Confa,Confb):-
378 glissement(P,D,Confa,Confb),
381 write('depl : '),writeln(DT3).
384 gl_ok(P,D,Confa,Confb,Lig,Col,[]):-
385 glissement(P,D,Confa,Confb,Lig,Col).
386 gl_ok(_,D,Confa,Confb,Lig,Col,[((Lp,Cp),Dp)|_]):-
387 glissement(_,D,Confa,Confb,Lig,Col),
388 not(retour(Lig,Col,D,Lp,Cp,Dp)).
391 /* Tous les successeurs d une configuration */
392 successeurs(Confa-Chemins,L):-
394 Confb-[((Lig,Col),D)|Chemins],
395 /*gl_ok(_,D,Confa,Confb,Lig,Col,Chemins),*/
396 glissement(_,D,Confa,Confb,Lig,Col),
399 successeurs_liste(L,Sl):-
400 maplist(successeurs,L,Lp),
402 list_to_assoc(Lpp,A),
407 /* regarde si le premier parametre appartient
408 à la liste donné en second parametre. Si c est le cas,
409 retourne le chemin */
416 appartient(Conf1-_,[Conf1-_|_]):-!.
417 appartient(Conf1-C,[_|L]):-
418 appartient(Conf1-C,L).
422 difference([El|L1],L2,Res):-
425 difference(L1,L2,Res).
426 difference([El|L1],L2,[El|Res]):-
427 difference(L1,L2,Res).
434 but([Conf-Chemin|_],Chemin):-final(Conf),
436 but([_|L],Chemin):-but(L,Chemin).
439 largeur(Atraiter,_,_,Res):-
442 largeur(Atraiter,Visites,C,Res):-
444 write('boucle'), writeln(C),
446 successeurs_liste(Atraiter,Succ),
448 write('taille succ : '),writeln(Taille),
451 append(Atraiter,Visites,Visites2),
453 difference(Succ,Visites2,Atraiter2),
456 write('successeur : '),writeln(DT1),
457 write('difference : '),writeln(DT3),
458 largeur(Atraiter2,Visites2,Cp,Res).
462 largeur([X-[]],[],0,Res).
472 ja-[1-(0,0),2-(3,0)],
474 no-[1-(0,2),2-(3,2)],
476 ma-[1-(0,4),2-(3,4)],
477 bo-[1-(1,3),2-(2,3)],
478 vi-[1-(1,4),2-(2,4)]],
482 gen_assoc(no,X,[1-(1,3),2-(3,3)]),
483 gen_assoc(bl,X,[1-(1,2)]),
484 gen_assoc(ma,X,[1-(0,3),_]).
487 gen_assoc(no,X,[1-(1,3),2-(2,3)]).
493 gen_assoc(ja,X,[1-(0,0),2-(3,0)]),
494 gen_assoc(ro,X,[1-(1,0)]),
495 gen_assoc(no,X,[1-(1,3),2-(3,3)]),
496 gen_assoc(bl,X,[1-(0,2)]),
497 gen_assoc(ma,X,[1-(0,3),2-(2,4)]),
498 gen_assoc(bo,X,[1-(0,4),2-(3,2)]).