15 puzzle solver: Difference between revisions

no edit summary
No edit summary
Line 5,795:
drrrdlluurrdddlluururddlurddlulldrrulluurrdrdllluurrdrdllldruururdddlluluurrrddd
</pre>
 
=={{header|Picat}}==
<lang Picat>import planner.
 
main =>
init(InitS),
goal(GoalS),
best_plan((InitS,GoalS),Plan),
println(Plan).
 
init(InitS) =>
M = {{15, 14, 1, 6},
{9 , 11, 4, 12},
{0, 10, 7, 3},
{13, 8, 5, 2}},
InitS = [(R,C) : T in 0..15, pos(M,T,R,C)].
 
goal(GoalS) =>
M = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13,14, 15, 0}},
GoalS = [(R,C) : T in 0..15, pos(M,T,R,C)].
 
pos(M,T,R,C) =>
N = len(M),
between(1,N,R),
between(1,N,C),
M[R,C] == T,!.
final((S,GoalS)) => S == GoalS.
 
action((S,GoalS),NextS,Action,Cost) =>
S = [P0|Tiles],
P0 = (R0,C0),
Cost = 1,
(R1 = R0-1, R1 >= 1, C1 = C0, Action = u;
R1 = R0+1, R1 =< 4, C1 = C0, Action = d;
R1 = R0, C1 = C0-1, C1 >= 1, Action = l;
R1 = R0, C1 = C0+1, C1 =< 4, Action = r),
P1 = (R1,C1),
slide(P0,P1,Tiles,Tiles1),
S1 = [P1|Tiles1],
NextS = (S1,GoalS).
 
% slide the tile at P1 to the empty square at P0
slide(P0,P1,[P1|Tiles],Tiles1) =>
Tiles1 = [P0|Tiles].
slide(P0,P1,[Tile|Tiles],Tiles1) =>
Tiles1=[Tile|Tiles1R],
slide(P0,P1,Tiles,Tiles1R).
 
% called by the planner
heuristic((S,GoalS)) = Dist =>
S = [_|Tiles],
GoalS = [_|FTiles],
Dist = sum([abs(R-FR)+abs(C-FC) :
{(R,C),(FR,FC)} in zip(Tiles,FTiles)]).
 
</lang>
 
=={{header|Perl}}==
Anonymous user