首页 > 代码库 > 一个例子,关于航班线路的深度优先搜索

一个例子,关于航班线路的深度优先搜索

  1 java 代码,摘自《java 编程艺术》
  2 
  3 /**
  4 * 航班信息类
  5 * 用于存放航班线路
  6 * @author shiyan
  7 *
  8 */
  9 public class FlightInfo {
 10 String from;//出发城市
 11 String to;//目的城市
 12 int distance;//距离
 13 boolean skip;//回退标志
 14 public FlightInfo(String f,String t,int d){
 15 this.from=f;
 16 this.to=t;
 17 this.distance=d;
 18 skip=false;
 19 }
 20 }
 21 
 22 
 23 
 24 import java.io.BufferedReader;
 25 import java.io.IOException;
 26 import java.io.InputStreamReader;
 27 import java.util.Stack;
 28 
 29 /**
 30 * 深度优先搜索
 31 * @author shiyan
 32 *
 33 */
 34 public class Depth {
 35 final int Max=100;
 36 FlightInfo flights[]=new FlightInfo[Max];//航班信息
 37 int numFlights=0;//实际航班个数
 38 Stack btStack=new Stack();//回退标志
 39 public static void main(String[] args){
 40 String to,from;
 41 Depth ob=new Depth();
 42 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 43 ob.setup();
 44 try{
 45 System.out.println("From? ");
 46 from=br.readLine();
 47 System.out.print("To? ");
 48 to=br.readLine();
 49 
 50 ob.isFlight(from, to);
 51 if(ob.btStack.size()!=0)
 52 ob.route(to);
 53 }catch(IOException e){
 54 System.out.println("Error on input.");
 55 }
 56 }
 57 public    void setup(){
 58 addFlight("纽约","芝加哥", 900);
 59 addFlight("芝加哥","丹佛", 1000);
 60 addFlight("纽约","多伦多", 500);
 61 addFlight("多伦多","卡尔加里", 1800);
 62 addFlight("纽约","丹佛", 1700);
 63 addFlight("多伦多","洛杉机", 2500);
 64 addFlight("多伦多","芝加哥", 500);
 65 addFlight("丹佛","乌儿巴纳", 1000);
 66 addFlight("丹佛","休斯顿", 1000);
 67 addFlight("休斯顿","洛杉机", 1500);
 68 addFlight("丹佛","洛杉机", 1000);
 69 }
 70 
 71 public void addFlight(String from,String to,int dist){
 72 if(numFlights<Max){
 73 flights[numFlights]=new FlightInfo(from,to,dist);
 74 numFlights++;
 75 }else 
 76 System.out.println("Flight database full.");
 77 }
 78 /**
 79 * 确定两个城市之间是否有航班,有则返回距离,没有则返回0
 80 * @param from
 81 * @param to
 82 * @return
 83 */
 84 public int match(String from,String to){
 85 for(FlightInfo flight:flights){
 86 if(flight!=null){
 87 if(flight.from.equals(from) && flight.to.equals(to) && !flight.skip){
 88 flight.skip=true;//避免再次使用
 89 return flight.distance;
 90 }
 91 }
 92 }
 93 /*for(int i=0;i<numFlights;i++){
 94 if(flights[i].from.equals(from) && flights[i].to.equals(to) && !flights[i].skip){
 95 flights[i].skip=true;
 96 return flights[i].distance;
 97 }
 98 }*/
 99 return 0;
100 }
101 
102 /**
103 * 查询from有没有到其他任何城市的航班
104 * @param from
105 * @return
106 */
107 public FlightInfo find(String from){
108 for(FlightInfo flight:flights){
109 if(flight!=null){
110 if(flight.from.equals(from) && !flight.skip){
111 FlightInfo f=new FlightInfo(flight.from,flight.to,flight.distance);
112 flight.skip=true;//避免重复使用
113 return f;
114 }
115 }
116 }
117 return null;
118 }
119 
120 /**
121 * 查询两个城市之间线路
122 * @param from
123 * @param to
124 */
125 public void isFlight(String from,String to){
126 int dist;
127 FlightInfo f;
128 dist=match(from,to);
129 if(dist!=0){//有直达
130 btStack.push(new FlightInfo(from,to,dist));
131 return;
132 }
133 f=find(from);
134 if(f!=null){
135 btStack.push(new FlightInfo(from,to,f.distance));
136 isFlight(f.to,to);
137 }else if(btStack.size()>0){
138 //回退,尝试其它路径
139 f=(FlightInfo) btStack.pop();
140 isFlight(f.from,f.to);
141 }
142 }
143 
144 /**
145 * 显示路径和总长度
146 * @param to
147 */
148 public void route(String to){
149 Stack rev=new Stack();
150 int dist=0;
151 FlightInfo f;
152 int num=btStack.size();
153 
154 //反转btStack(里面的路径是相反的,栈的顶部保存的是最后一个航班信息,底部是第一个航班信息),从起点到终点显示路径将路径压栈rev中
155 for(int i=0;i<num;i++){
156 rev.push(btStack.pop());
157 }
158 
159 for(int i=0;i<num;i++){
160 f=(FlightInfo)rev.pop();
161 System.out.print(f.from+" to ");
162 dist+=f.distance;
163 }
164 System.out.println(to);
165 System.out.println("Distance is "+dist);
166 }
167 
168 }
169 
170  

 

一个例子,关于航班线路的深度优先搜索