prolog maze question The following is the layout of a simple maze I door (a, b)
ID: 3828843 • Letter: P
Question
prolog maze question
The following is the layout of a simple maze I door (a, b) door (b c) door (c d) door (d e). door (e, b) door (e, f). door (e, g). door (k j). trap (h, g). trap (h b) trap (g, j) write a predicate path (x, Y, P) that is true if P is the list of rooms to be traversed to go from room X to room Y write a predicate noway (x, Y) that is true if and only if there is no way to go from room X to room Y write a predicate Redundant (X Y) that is true if a door or trap from X to Y could be removed from the maze without affecting the existence of a path from any room from any other room. that is true if the room x cannot be accessed write a predicate isolated (X) ss from any other room in the maze. Write a predicate deadlyTrap (x, Y) which is true if the room Y can be reached from the room X, but there is no way to get backExplanation / Answer
%% Write a predicate path(X, Y, P) that is true if P is the list of rooms to be traversed
%% to go from room X to room Y.
connected(X,Y) :- door(X,Y); door(Y,X); trap(X,Y).
path(X,Y,P) :-
travel(X,Y,[X],Q),
reverse(Q,P).
travel(X,Y,P,[Y|P]) :-
connected(X,Y).
travel(X,Y,Visited,P) :-
connected(X,Z),
Z == Y,
not(member(Z,Visited)),
travel(Z,Y,[Z|Visited],P).
%% Write a predicate noway(X, Y) that is true if and only if there is no way to
%% go from room X to room Y.
noway(X, Y) :- not(path(X,Y,P)).
%% Write a predicate Redundant(X, Y) that is true if a door or trap from X to Y
%% could be removed from the maze without affecting the existence of a path
%% from any room from any other room.
redundant(X,Y):- path(X,Y,A), !, path(X,Y,B), A == B, !.
%% write a predicate isolated(X) that is true if the room X cannot be accessed
%% from any other room in the maze.
door2(X,Y) :- door(X,Y); door(Y,X).
trap2(X,Y) :- trap(Y,X).
isolated(X) :- not(door2(X,Y)), not(trap2(X,Y)).
%% Write a predicate deadlyTrap(X, Y) which is true if the room Y can be reached from the
%% room X, but there is no way to get back.
deadlyTrap(X,Y) :- path(X,Y,P), !, noway(Y,X).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.