-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab4.pl
38 lines (28 loc) · 866 Bytes
/
lab4.pl
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
append([],B,B).
append([H|Tail],B,[H|NewTail]):-
append(Tail,B,NewTail).
swap(A,B):-
append(Begin,['_','w'|Tail],A),
append(Begin,['w','_'|Tail],B).
swap(A,B):-
append(Begin,['b','_'|Tail],A),
append(Begin,['_','b'|Tail],B).
swap(A,B):-
append(Begin,['_','b','w'|Tail],A),
append(Begin,['w','b','_'|Tail],B).
swap(A,B):-
append(Begin,['b','w','_'|Tail],A),
append(Begin,['_','w','b'|Tail],B).
goal(['w','w','w','_','b','b','b']).
solve( Start, Solution) :-
bfs( [ [Start] ], Solution).
bfs( [ [Node | Path] | _ ], [Node | Path] ) :-
goal( Node).
bfs( [ [N | Path] | Paths], Solution ) :-
bagof( [N1, N | Path ],
( swap( N, N1), not(member( N1, [N | Path]))),
NewPaths),
append( Paths, NewPaths, Paths1), !,
bfs( Paths1, Solution);
bfs( Paths, Solution).
?- solve(['b','b','b','_','w','w','w'], X).