Shortest distance between cities Prolog source program

Source: Internet
Author: User

A work title: known to Beijing, Shanghai, Guangzhou, Kunming and Xi ' an five city route map. To achieve: 1) find the shortest route from Beijing to Kunming. 2) to find Beijing to Kunming, must approach the path of Shanghai. 3) find Beijing to Kunming, must approach Shanghai, but can not go to the path of Guangzhou.

 

Beijing

Shanghai

Guangzhou

Kunming

Xian

Beijing

 

 

 

 

 

Shanghai

1462

 

 

 

 

Guangzhou

2107

1811

 

 

 

Kunming 3179 2677 2216

 

 

Xian 1165 1511 2129 1942

 

Visual Prolog source program: Domains
City=symbol
citylist=city*
Distance=integer
Predicates
Nondeterm Road (City,city,distance)
Nondeterm Connected (city,city,distance)
Nondeterm Route (city,city,citylist,citylist,distance)
Member (City,citylist)
Showallroutes (city,city)
Shorterrouteexist (city,city,distance)
Showshortestroute (city,city)
Showmustpassroutes (city,city,city)
Showmustnotpassroutes (city,city,city,city)
Writeroute (city,citylist,distance)
Reverse_list (Citylist,citylist,citylist)
Clauses
Road (beijing,shanghai,1462). Road (beijing,guangzhou,2107).
Road (beijing,kunming,3179). Road (beijing,xi_an,1165).
Road (shanghai,guangzhou,1811). Road (shanghai,kunmig,2677).
Road (shanghai,xi_an,1511). Road (guangzhou,kunming,2216).
Road (guangzhou,xi_an,2129). Road (kunming,xi_an,1942).

Connected (city1,city2,distance):-road (city1,city2,distance).
Connected (city1,city2,distance):-road (city2,city1,distance).

Member (X,[x|_]):-!.
Member (x,[_| L]):-member (x,l).

Route (city,city,visitedcities,visitedcities,0):-!.
Route (city1,city2,visitedcities,resultvisitedcities,distance):-
Connected (City1,x,distance1),
Not (Member (x,visitedcities)),
Route (x,city2,[x| VISITEDCITIES],RESULTVISITEDCITIES,DISTANCE2),
Distance=distance1+distance2.
Showallroutes (city1,city2):-
Write ("All routes between", City1, "and", City2, "are:/n"),
Route (City1,city2,[city1],visitedcities,distance),
Writeroute (city1,visitedcities,distance), NL,
Fail.
Showallroutes (_,_).

Showmustpassroutes (city1,city2,city3):-
Write ("All routes between", City1, "and", City2, "must Pass", City3, "are:/n/n"),
Route (City1,city2,[city1],visitedcities,distance),
Member (City3,visitedcities),
Writeroute (city1,visitedcities,distance), NL,
Fail.
Showmustpassroutes (_,_,_).

Showmustnotpassroutes (CITY1,CITY2,CITY3,CITY4):-
Write ("All routes between", City1, "and", City2, "must Pass", City3, "but does ' nt pass", City4, "are:/n/n"),
Route (City1,city2,[city1],visitedcities,distance),
Member (City3,visitedcities),
Not (Member (city4,visitedcities)),
Writeroute (city1,visitedcities,distance), NL,
Fail.
Showmustnotpassroutes (_,_,_,_).

Shorterrouteexist (city1,city2,distance):-
Route (City1,city2,[city1],_,distance1),
Distance1<distance,!.

Showshortestroute (city1,city2):-
Write ("Shortest route between", City1, "and", City2, "is:/n/n"),
Route (City1,city2,[city1],visitedcities,distance),
Not (Shorterrouteexist (city1,city2,distance)),
Writeroute (city1,visitedcities,distance), NL,
Fail.
Showshortestroute (_,_).

Writeroute (city1,[city1| Visitedcities],distance):-
!,
cities=[city1| Visitedcities],
Write ("", Cities, "The distance is", distance), NL.
Writeroute (_,visitedcities,distance):-
Reverse_list (visitedcities,[],visitedcities_reversed),
Write ("", visitedcities_reversed, "The distance is", distance), NL.

Reverse_list ([],list,list):-!.
Reverse_list ([h| Seenlistreset],interm,seenlist):-
Reverse_list (seenlistreset,[h| Interm],seenlist). Goal
Showshortestroute (beijing,kunming),
Showmustpassroutes (Beijing,kunming,shanghai),
Showmustnotpassroutes (Beijing,kunming,shanghai,guangzhou).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.