An example of depth-first search for flight lines

Source: Internet
Author: User

1 Java code, excerpt from Java programming Art2 3 /**4 * Flight Information class5 * For storage of flight lines6 * @authorShiyan7 *8 */9  Public classFlightinfo {TenString from;//Departure City OneString to;//Destination City A intDistance//Distance - BooleanSkip//Fallback Flag -  PublicFlightinfo (String f,string T,intd) { the  This. from=F; -  This. to=T; -  This. distance=D; -skip=false; + } - } +  A  at  - ImportJava.io.BufferedReader; - Importjava.io.IOException; - ImportJava.io.InputStreamReader; - ImportJava.util.Stack; -  in /** - * Depth-First search to * @authorShiyan + * - */ the  Public classDepth { * Final intmax=100; $Flightinfo flights[]=NewFlightinfo[max];//Flight InformationPanax Notoginseng intnumflights=0;//actual number of flights -Stack btstack=NewStack ();//Fallback Flag the  Public Static voidMain (string[] args) { + String To,from; ADepth ob=NewDepth (); theBufferedReader br=NewBufferedReader (NewInputStreamReader (system.in)); + Ob.setup (); - Try{ $System.out.println ("From?"); $From=br.readline (); -System.out.print ("to?"); -to=br.readline (); the  - Ob.isflight (from, to);Wuyi if(Ob.btStack.size ()!=0) the Ob.route (to); -}Catch(IOException e) { WuSystem.out.println ("Error on input."); - } About } $  Public    voidSetup () { -Addflight ("New York", "Chicago", 900); -Addflight ("Chicago", "Denver", 1000); -Addflight ("New York", "Toronto", 500); AAddflight ("Toronto", "Calgary", 1800); +Addflight ("New York", "Denver", 1700); theAddflight ("Toronto", "Los Angeles", 2500); -Addflight ("Toronto", "Chicago", 500); $Addflight ("Denver", "Ufa", 1000); theAddflight ("Denver", "Houston", 1000); theAddflight ("Houston", "Los Angeles", 1500); theAddflight ("Denver", "Los Angeles", 1000); the } -  in  Public voidAddflight (String from,string to,intDist) { the if(numflights<Max) { theflights[numflights]=NewFlightinfo (from,to,dist); Aboutnumflights++; the}Else  theSYSTEM.OUT.PRINTLN ("Flight database Full.")); the } + /** - * Determine if there is a flight between two cities, the return distance, or 0 the * @param fromBayi * @param to the * @return the */ -  Public intMatch (String from,string to) { -  for(Flightinfo flight:flights) { the if(flight!=NULL){ the if(Flight.from.equals (from) && flight.to.equals (to) &&!Flight.skip) { theflight.skip=true;//avoid re-use the returnflight.distance; - } the } the } the /*for (int i=0;i<numflights;i++) {94 if (Flights[i].from.equals (from) && flights[i].to.equals (to) &&!flights[i].skip) { the flights[i].skip=true; the return flights[i].distance; the }98 }*/ About return0; - }101 102 /**103 * Check from there are no flights to any other city104 * @param from the * @return106 */107  PublicFlightinfo Find (String from) {108  for(Flightinfo flight:flights) {109 if(flight!=NULL){ the if(Flight.from.equals (from) &&!Flight.skip) {111Flightinfo f=NewFlightinfo (flight.from,flight.to,flight.distance); theflight.skip=true;//avoid re-use113 returnF; the } the } the }117 return NULL;118 }119  - /**121 * Check the route between two cities122 * @param from123 * @param to124 */ the  Public voidisflight (String from,string to) {126 intDist;127 Flightinfo F; -dist=match (from,to);129 if(dist!=0) {//with direct theBtstack.push (NewFlightinfo (from,to,dist));131 return; the }133f=find (from);134 if(f!=NULL){135Btstack.push (NewFlightinfo (from,to,f.distance));136 isflight (f.to,to);137}Else if(Btstack.size () >0){138 //Fallback , try another path139f=(Flightinfo) Btstack.pop (); $ isflight (f.from,f.to);141 }142 }143 144 /**145 * Show path and total length146 * @param to147 */148  Public voidRoute (String to) {149Stack rev=NewStack (); Max intDist=0;151 Flightinfo F; the intnum=btstack.size ();153 154 //Reverse Btstack (the path inside is reversed, the top of the stack is the last flight information, the bottom is the first flight information), and the path from the start to the end shows the path is pressed in Rev155  for(inti=0;i<num;i++){156 Rev.push (Btstack.pop ());157 }158 159  for(inti=0;i<num;i++){ thef=(Flightinfo) Rev.pop ();161System.out.print (f.from+ "to");162dist+=f.distance;163 }164 System.out.println (to);165System.out.println ("Distance is" +Dist);166 }167 168 }169  the  

An example of depth-first search for flight lines

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.