How can I write rules for the following questions

2019-09-13 01:07发布

问题:

  1. Write a Prolog rule to find any route between two stations.

    route(Station1, Station2, Route)

where Station1 and Station2 are station names and Route is a list of stations connecting Station1 and Station2 in order, including both Station1 and Station2.

  1. Write a Prolog rule to find the time taken to travel on a route between two stations, on the assumption that the time to travel between adjacent stations is 4 minutes. Ignore any time associated with changing lines at interchanges.

    route_time(Station1, Station2, Route, Minutes)

where Station1 and Station2 are station names, Route is a list of stations connecting Station1 and Station2 in order, including both Station1 and Station2, and Minutes is the time taken.

station(al,[metropolitan]).
station(ls,[metropolitan,central]).
station(kx,[metropolitan,victoria]).
station(bs,[metropolitan]).
station(fr,[metropolitan]).
station(ke,[northern]).
station(em,[northern,bakerloo]).
station(tc,[northern,central]).
station(ws,[northern,victoria]).
station(eu,[northern]).
station(wa,[bakerloo]).
station(pa,[bakerloo]).
station(oc,[bakerloo,central,victoria]).
station(ec,[bakerloo]).
station(nh,[central]).
station(lg,[central]).
station(cl,[central]).
station(bg,[central]).
station(br,[victoria]).
station(vi,[victoria]).
station(fp,[victoria]).


/* Facts about the direct connection between two stations.  
   The adjacent rule shows the connections between two stations */

adjacent1(X,Y):- adjacent(X,Y);adjacent(Y,X).

/* The first rule is X is adjacent Y IF and only IF   
   X is adjacent to Y and Y is adjacent to X */


/* Bakerloo Line adjacents*/

adjacent(em,ec).
adjacent(em,ke).
adjacent(em,tc).
adjacent(em,oc).
adjacent(oc,vi).
adjacent(oc,ws).
adjacent(oc,pa).
adjacent(oc,lg).
adjacent(oc,tc).
adjacent(pa,wa).


/* Central Line Adjacents*/

adjacent(bg,ls).
adjacent(ls,al).
adjacent(ls,kx).
adjacent(ls,cl).
adjacent(cl,tc).
adjacent(lg,nh).


/*Metro adjacents*/

adjacent(kx,fp).
adjacent(kx,ws).
adjacent(kx,bs).
adjacent(bs,fr).


 /* Northern Line*/

adjacent(tc,ws).
adjacent(ws,eu).


/*Victoria line */

adjacent(br,vi).
标签: prolog